Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/agents/models/openai_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ def __init__(
def _non_null_or_omit(self, value: Any) -> Any:
return value if value is not None else omit

def _uses_official_openai_endpoint(self) -> bool:
return is_official_openai_client(self._get_client())

def _supports_default_prompt_cache_key(self) -> bool:
return is_official_openai_client(self._get_client())

Expand Down Expand Up @@ -572,6 +575,11 @@ async def get_response(
) -> ModelResponse:
with response_span(disabled=tracing.is_disabled()) as span_response:
try:
redacted_response_id_endpoint_is_trusted = (
not tracing.include_data()
and not tracing.is_disabled()
and self._uses_official_openai_endpoint()
)
response = await self._fetch_response(
system_instructions,
input,
Expand Down Expand Up @@ -604,6 +612,11 @@ async def get_response(
if tracing.include_data():
span_response.span_data.response = response
span_response.span_data.input = input
elif (
redacted_response_id_endpoint_is_trusted
and self._uses_official_openai_endpoint()
):
span_response.span_data._response_id = response.id
except asyncio.CancelledError:
record_current_task_model_timeout_on_span(
span_response,
Expand Down Expand Up @@ -658,6 +671,11 @@ async def stream_response(
"""
with response_span(disabled=tracing.is_disabled()) as span_response:
try:
redacted_response_id_endpoint_is_trusted = (
not tracing.include_data()
and not tracing.is_disabled()
and self._uses_official_openai_endpoint()
)
stream = await self._fetch_response(
system_instructions,
input,
Expand All @@ -680,6 +698,11 @@ async def stream_response(
chunk_type = getattr(chunk, "type", None)
if isinstance(chunk, ResponseCompletedEvent):
final_response = chunk.response
if (
redacted_response_id_endpoint_is_trusted
and self._uses_official_openai_endpoint()
):
span_response.span_data._response_id = chunk.response.id
if model_settings.preserve_raw_usage is True:
_attach_raw_usage_snapshot(chunk.response, chunk.response.usage)
usage = _usage_from_response(chunk.response)
Expand Down Expand Up @@ -1111,6 +1134,13 @@ def __init__(
)
self._ws_client_close_generation = 0

def _uses_official_openai_endpoint(self) -> bool:
base_url = prepare_openai_client_websocket_base_url(
self._client,
context="Responses websocket",
)
return is_official_openai_base_url(base_url, websocket=True)

def _supports_default_prompt_cache_key(self) -> bool:
if self._client.websocket_base_url is not None:
return is_official_openai_base_url(self._client.websocket_base_url, websocket=True)
Expand Down
5 changes: 3 additions & 2 deletions src/agents/tracing/span_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class ResponseSpanData(SpanData):
Includes response and input.
"""

__slots__ = ("response", "input", "usage")
__slots__ = ("response", "input", "usage", "_response_id")

def __init__(
self,
Expand All @@ -228,6 +228,7 @@ def __init__(
# processor implementations
self.input = input
self.usage = usage
self._response_id: str | None = None

@property
def type(self) -> str:
Expand All @@ -236,7 +237,7 @@ def type(self) -> str:
def export(self) -> dict[str, Any]:
return {
"type": self.type,
"response_id": self.response.id if self.response is not None else None,
"response_id": (self.response.id if self.response is not None else self._response_id),
"usage": self.usage,
}

Expand Down
Loading
Loading