Skip to content

feat(tracing): accept metadata kwarg on all span factories#3155

Open
srikalyan wants to merge 1 commit intoopenai:mainfrom
srikalyan:feat/span-factory-metadata
Open

feat(tracing): accept metadata kwarg on all span factories#3155
srikalyan wants to merge 1 commit intoopenai:mainfrom
srikalyan:feat/span-factory-metadata

Conversation

@srikalyan
Copy link
Copy Markdown

@srikalyan srikalyan commented May 6, 2026

Summary

Extend the optional metadata kwarg to every public *_span() factory and the matching SpanData subclasses that lacked it (function, generation, response, handoff, custom, guardrail, transcription, speech, speech_group, mcp_tools). Append the new parameter at the end of each signature to preserve positional compatibility.

The exporter already serializes span.span_data.metadata at the top-level metadata key, so no changes are needed in the export pipeline. Backfill the same short Args entry in every helper docstring that documents arguments, so the generated reference picks it up.

Test plan

  • Added a parametrized test in tests/test_tracing.py that round-trips a metadata dict through every public span factory (13 ids) and verifies it surfaces under the exported payload's top-level metadata field.
  • Added a test that confirms span.span_data.metadata can be mutated after construction (e.g. via a direct span reference) and the new value is reflected in the export.
  • Local verification:
    • make format — clean
    • make lint — clean
    • uv run mypy src/agents/tracing/span_data.py src/agents/tracing/create.py tests/test_tracing.py — clean
    • uv run pytest tests/test_tracing.py tests/test_agent_tracing.py tests/test_responses_tracing.py tests/test_function_tool.py — 105 passed

Issue number

Partially addresses #1844. Does not Close #1844, because the active response_span during LLM hooks is still a separate runtime-ordering problem.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

@github-actions github-actions Bot added enhancement New feature or request feature:tracing labels May 6, 2026
@seratch
Copy link
Copy Markdown
Member

seratch commented May 6, 2026

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ 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".

Extend the optional `metadata` kwarg to every public `*_span()` factory and the matching SpanData subclasses that lacked it (function, generation, response, handoff, custom, guardrail, transcription, speech, speech_group, mcp_tools). Append the new parameter at the end of each signature to preserve positional compatibility.

The exporter already serializes `span.span_data.metadata` at the top-level `metadata` key, so no changes are needed in the export pipeline. Backfill the same short Args entry in every helper docstring that documents arguments, so the generated reference picks it up.

Note: Partially addresses openai#1844. Does not Close openai#1844, because the active response_span during LLM hooks is still a separate runtime-ordering problem
@srikalyan srikalyan force-pushed the feat/span-factory-metadata branch from 90d9682 to 1b3e9f5 Compare May 7, 2026 18:49
@srikalyan
Copy link
Copy Markdown
Author

@codex review

@seratch
Copy link
Copy Markdown
Member

seratch commented May 8, 2026

Thanks for sharing this. I don't have the bandwidth to check this suggestion in depth right now, but have you verified if this change works well with the production OpenAI production API endpoint for tracing? The API could reject some data, so verifying the code alone may not be enough for quality assurance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request feature:tracing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Customizing traces and spans: Ability to add metadata to spans

2 participants