Skip to content

OpenAI: Agents session stream get_final_result() / with_result_collection() skip tracing #859

Closed

Description

openai 3.23.0 made client.beta.agents.sessions.create(stream=True) return AgentSessionEventStream, which adds with_result_collection() and get_final_result() (and an output_type= kwarg on create).

Our _TracedStream / _AsyncTracedStream wrappers in py/src/braintrust/integrations/openai/tracing.py (returned from AgentSessionWrapper.create / acreate) are NamedWrappers whose __getattr__ forwards unknown attributes to the real stream. So:

  • stream.get_final_result() runs on the real stream, which iterates itself and calls close(). _AgentSessionTrace.observe() / finish() never run, so the openai.agents.sessions.create span gets no output, metrics or tool spans and is never ended.
  • stream.with_result_collection() returns the real stream (self), so iterating its return value also skips tracing.

Fix

  • Implement get_final_result() on the traced wrappers (or a session-specific subclass): drain through the traced generator, then call the wrapped stream's get_final_result(). Optionally log result.output as the span output.
  • Make with_result_collection() call the wrapped method and return the traced wrapper.
  • Add a cassette-backed test next to the existing session stream tests in test_openai.py. Gate it on openai >= 3.23.0.

Parent: #858 (found while reviewing the 2026-10-05 weekly dependency update, #849).

🤖 Generated with Claude Code

Metadata

Metadata

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions