Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/agents/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def _truncate_span_field_value(self, value: Any) -> Any:

return self._truncate_json_value_for_limit(sanitized_value, max_bytes)

_TRUNCATED_PREVIEW_KEY = "__truncated_preview__"

def _truncate_json_value_for_limit(self, value: Any, max_bytes: int) -> Any:
if self._value_json_size_bytes(value) <= max_bytes:
return value
Expand All @@ -291,6 +293,14 @@ def _truncate_json_value_for_limit(self, value: Any, max_bytes: int) -> Any:
return self._truncate_string_for_json_limit(value, max_bytes)

if isinstance(value, dict):
# Avoid re-truncating our own truncated preview dicts, which
# would cause infinite recursion (the preview dict contains bool
# values that get wrapped into new preview dicts, and so on).
# Use a sentinel key to reliably identify SDK-generated previews
# without matching arbitrary user data that happens to use the
# same key names.
if value.get(self._TRUNCATED_PREVIEW_KEY) is True and "original_type" in value and "preview" in value:
return value
Comment on lines +302 to +303
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict preview bypass to SDK-owned objects

Fresh evidence: although this guard now also checks original_type and preview, it still short-circuits on any user-supplied dict that includes "__truncated_preview__": true and those keys. If that dict also contains large extra fields and exceeds _OPENAI_TRACING_MAX_FIELD_BYTES, _truncate_json_value_for_limit returns it unchanged instead of delegating to _truncate_mapping_for_json_limit, so sanitization can still emit oversized span fields that get rejected or dropped by trace ingestion.

Useful? React with 👍 / 👎.

return self._truncate_mapping_for_json_limit(value, max_bytes)

if isinstance(value, list):
Expand Down Expand Up @@ -356,6 +366,7 @@ def _truncated_preview(self, value: Any) -> dict[str, Any]:
preview = f"<{type_name} bytes={len(value)} truncated>"

return {
self._TRUNCATED_PREVIEW_KEY: True,
"truncated": True,
"original_type": type_name,
"preview": preview,
Expand Down