feat: collect final output from beta Agents streams - #4004
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
Security findingsAdvisory findings (1)
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e541046804
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
🛡️ Codex Security Review · Automatically triggered
Here are some automated security review suggestions for this pull request.
Reviewed commit: e541046804
ℹ️ About Codex security reviews in GitHub
This is an experimental Codex feature. Security reviews are triggered when:
- You comment "@codex security review"
- A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review
Once complete, Codex will leave suggestions, or a comment if no findings are found.
|
@codex security review |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
Castiron custom code✅ No new custom-code files detected. 49 mixed files remain; 1 existing customization changed. Compared
48 existing customizations unchanged
8 more in the full report. A changed generated baseline means this report cannot reliably identify which handwritten lines changed. Inspect the custom-code diffDownload the exact patch produced by this run (requires repository access): gh run download 36791993867 --repo openai/openai-python \
--name castiron-custom-code-36791993867-1 --dir /tmp/castiron-custom-code-36791993867-1
git apply --stat /tmp/castiron-custom-code-36791993867-1/custom-code.patch
cat /tmp/castiron-custom-code-36791993867-1/custom-code.patchOr reproduce it from an SDK checkout containing the vendored reporter: git fetch --no-tags origin 8a136c203062b999349da745226ba5299709c7be 33c9a07e0151a1b776648a9eacc739485fab795c
python3 scripts/castiron/custom_code_report.py report \
--base 8a136c203062b999349da745226ba5299709c7be \
--head 33c9a07e0151a1b776648a9eacc739485fab795c --fetch --require-head-hash --public \
--out /tmp/castiron-custom-code-33c9a07e0151
cat /tmp/castiron-custom-code-33c9a07e0151/custom-code.patchThis is the current full custom patch for mixed files, not an attribution of only the handwritten lines changed by this PR. |
## Summary
Bind a Pydantic output model to the Agents request and its completed
result, so applications can use a typed answer without maintaining
separate schema and parsing code.
Before:
```python
with client.beta.agents.sessions.create(
agent={"model": MODEL, "text": {"format": {
"type": "json_schema", "schema": schema,
}}},
environment={"type": "none"}, input=QUESTION, stream=True,
) as stream:
result = stream.get_final_result()
report = Report.model_validate_json(result.output_text)
```
After:
```python
with client.beta.agents.sessions.create(
agent={"model": MODEL}, environment={"type": "none"},
input=QUESTION, stream=True, output_type=Report,
) as stream:
result = stream.get_final_result()
report = result.output_parsed
```
The same `output_type` works on follow-up streams as a local parser for
an already-configured session. `output_parsed` returns the first parsed
final text part; all final text parts are validated. Raw output remains
available; parsing failures expose the completed result through
`AgentOutputParseError.result`. Sync and async helpers share the
existing Pydantic schema normalization and result collection.
Schema conversion follows the existing Responses helper; API errors
report unsupported schema features. Typed tools and outputs can reuse
the same model without output normalization changing its tool argument
schema.
### Stack
- #4004 (merged
prerequisite)
- #4007 👈 this PR
- #4008
- #4009
## Summary
Stage selected local files and read an artifact from the exact completed
turn, either in memory or streamed to a local path. The helpers keep
upload IDs available for explicit cleanup and stream downloads to a
caller-chosen path.
Before:
```python
uploaded = client.files.create(file=Path("source.pdf"), purpose="user_data")
inputs = [{"type": "file_id", "file_id": uploaded.id, "path": "/workspace/source.pdf"}]
# Pass inputs when creating the hosted environment, then run the session.
artifact = next(a for a in client.beta.agents.sessions.artifacts.list(result.session_id)
if a.turn_id == result.turn_id and a.path == "/workspace/outputs/report.md")
with client.beta.agents.sessions.artifacts.with_streaming_response.content(
artifact.id, session_id=result.session_id,
) as content:
content.stream_to_file("report.md")
```
After:
```python
prepared = client.beta.agents.environments.files.prepare({
"/workspace/source.pdf": Path("source.pdf"),
})
# Pass prepared.files when creating the hosted environment, then run the session.
artifacts = client.beta.agents.sessions.artifacts.for_result(result)
report_bytes = artifacts.content("/workspace/outputs/report.md").content
# Or stream to an application-owned path:
artifact = artifacts.download("/workspace/outputs/report.md", to=Path("report.md"))
```
`prepare_directory(..., include=[...])` selects a directory snapshot;
`files.upload(...)` uploads and stages one file in an existing
environment. Sync and async helpers live under the beta Agents
namespace. They preflight selected files before uploading, preserve
partial upload ownership on errors, and detect missing or ambiguous
artifacts across all pages.
Local path selection is intended for static application-owned files and
stable directories; it is not a filesystem sandbox for untrusted paths
or hostile local writers.
This PR now targets `main` directly. Reattachment/result recovery is
deferred: a silent attachment cannot reliably distinguish pending work
from an already-completed turn, so these helpers do not depend on
SDK-side recovery heuristics.
### Stack
- openai#4004 (merged
prerequisite)
- openai#4007 (merged
prerequisite)
- openai#4008 (closed/deferred;
not a dependency)
- openai#4009 👈 this PR
Summary
Collect a hosted agent's final answer directly from the stream, whether it creates a session or continues one. Applications can still display every event, but no longer need their own turn tracking and final-message accumulator.
Before:
After:
The beta result retains the generated turn and ordered final messages, including their content and annotations. Sync and async getters share selection rules and preserve existing tool dispatch. An unsuccessful turn, required action, or incomplete observation raises a beta result error with available partial state; closing the stream does not cancel hosted execution.
Ordinary event iteration remains incremental; progress consumers opt into result retention with
with_result_collection(). Creation streams retain their existingStreaminterface. Collection lives underopenai.lib.beta.agents; generated resource wiring only selects the specialized stream type.Stack: