Skip to content

fix: shared enum for gen_ai.operation.name values (#6416) - #7623

Closed
DawnofGenX wants to merge 1 commit into
getsentry:masterfrom
DawnofGenX:fix/issue-6416-genai-operation-enum
Closed

DawnofGenX wants to merge 1 commit into
getsentry:masterfrom
DawnofGenX:fix/issue-6416-genai-operation-enum

Conversation

@DawnofGenX

Copy link
Copy Markdown

Summary

Follow-up to the review discussion on #6404 (r3304429118): the gen_ai.operation.name span attribute values were hardcoded string literals scattered across the AI integrations (chat, execute_tool, invoke_agent, ...).

This PR introduces a shared GENAIOPERATION string enum in sentry_sdk/consts.py (following the existing SPANTEMPLATE(str, Enum) precedent) and replaces all 58 static literal usages SDK-wide with enum members:

  • sentry_sdk/consts.py — new GENAIOPERATION enum with the 8 values currently emitted by the SDK: CHAT, CREATE_AGENT, EMBEDDINGS, EXECUTE_TOOL, HANDOFF, INVOKE_AGENT, RESPONSES, TEXT_COMPLETION.
  • Replaced literals in: anthropic, openai, openai_agents (ai_client / execute_tool / handoff / invoke_agent), pydantic_ai (ai_client / execute_tool / invoke_agent), langchain, langgraph, litellm, mistral, google_genai, and tracing_utils (the @ai_track templates).
  • GENAIOPERATION is a str subclass with __str__ returning the value (identical semantics to SPANTEMPLATE), so f"{GENAIOPERATION.CHAT} model" (used by litellm span names) and every serializer path produce the exact same bytes as before.
  • The dynamic operation_name in huggingface_hub derives from the OP constant, so it is unchanged by design.

Wire values are byte-identical — this is a pure refactor, no behavior change.

Closes #6416

Wire-compatibility evidence

Ran the pydantic_ai integration suite (which asserts gen_ai.operation.name values on captured envelopes) on unpatched upstream/master and on this branch, same env, same command:

# BEFORE (upstream/master, changes stashed):
$ TESTPATH=tests/integrations/pydantic_ai/test_pydantic_ai.py uv run tox -e py3.14-pydantic_ai-latest
====================== 331 passed, 63 warnings in 23.32s =======================
  py3.14-pydantic_ai-latest: OK (25.96=setup[0.11]+cmd[25.85] seconds)
  congratulations :) (26.43 seconds)

# AFTER (this branch):
$ TESTPATH=tests/integrations/pydantic_ai/test_pydantic_ai.py uv run tox -e py3.14-pydantic_ai-latest
====================== 331 passed, 63 warnings in 23.03s =======================
  py3.14-pydantic_ai-latest: OK (25.75=setup[0.11]+cmd[25.63] seconds)
  congratulations :) (26.23 seconds)

Additionally verified serialization identity directly:

>>> json.dumps({"gen_ai.operation.name": GENAIOPERATION.CHAT}) == json.dumps({"gen_ai.operation.name": "chat"})
True
>>> msgpack.packb({"k": GENAIOPERATION.CHAT})   # b'\x81\xa1k\xa4chat'  — plain "chat" on the wire

Test plan

  • New tests/test_consts.py: pins every GENAIOPERATION member name and value to the exact wire strings, plus str()/f-string/json.dumps round-trips — a rename or revalue now fails CI (regression guard requested in Create an enum for possible gen_ai.operation.name values #6416).
  • py3.14-common (full core suite incl. new tests): 2252 passed, 174 skipped
  • py3.14-pydantic_ai-latest: 331 passed (before/after above)
  • py3.14-anthropic-latest: 587 passed
  • py3.14-openai_agents-latest: 305 passed
  • py3.14-openai-base-latest: 686 passed
  • py3.14-langchain-base-latest: 625 passed
  • py3.14-langgraph-v0.6.11: 146 passed
  • py3.14-litellm-latest: 171 passed
  • py3.14-mistral-latest: 88 passed
  • py3.14-google_genai-latest: 526 passed, 1 skipped
  • uv run ruff check --fix sentry_sdk tests → All checks passed
  • uv run ruff format sentry_sdk tests → formatted
  • uv run --group typing mypy sentry_sdkSuccess: no issues found in 194 source files

@DawnofGenX
DawnofGenX requested a review from a team as a code owner September 22, 2026 21:09
@alexander-alderman-webb

Copy link
Copy Markdown
Contributor

Going to close this because we don't have capacity to review the 5 PRs you've opened.
If you'd like to contribute dedicate your time towards making one good PR. Thanks.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create an enum for possible gen_ai.operation.name values

2 participants