SIGN IN SIGN UP

feat: collect final output from beta Agents streams (#4004)

## 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:

```python
with client.beta.agents.sessions.create(
    agent={"model": MODEL}, environment={"type": "none"},
    input="Explain this policy.", stream=True,
) as stream:
    for event in stream:
        # Track the root turn, gather completed final messages,
        # and distinguish completion from interrupted delivery.
        collect_event(event)

print(collected_answer())
```

After:

```python
with client.beta.agents.sessions.create(
    agent={"model": MODEL}, environment={"type": "none"},
    input="Explain this policy.", stream=True,
) as stream:
    result = stream.get_final_result()

print(result.output_text)

with client.beta.agents.sessions.stream(
    result.session_id, input="Give me an example.", tool_handlers=handlers,
).with_result_collection() as stream:
    for event in stream:
        show_progress(event)
    followup = stream.get_final_result()

print(followup.output_text)
```

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 existing `Stream` interface. Collection lives under
`openai.lib.beta.agents`; generated resource wiring only selects the
specialized stream type.
A
Alex Chang committed
157ac4c49a34891d5b66d2950b5082d1b3268315
Parent: 8a136c2
Committed by GitHub <noreply@github.com> on 9/30/2026, 11:43:57 PM