Skip to content

fix(otel): fix double-serialization of span attributes#493

Open
simonvdk-mistral wants to merge 1 commit intomainfrom
svdk/fix/otel_double_serialized_attributes
Open

fix(otel): fix double-serialization of span attributes#493
simonvdk-mistral wants to merge 1 commit intomainfrom
svdk/fix/otel_double_serialized_attributes

Conversation

@simonvdk-mistral
Copy link
Copy Markdown
Contributor

@simonvdk-mistral simonvdk-mistral commented Apr 17, 2026

Problem

gen_ai.input.messages, gen_ai.output.messages, and gen_ai.tool.definitions span attributes were double-serialized. Each message/definition was individually json.dumps()-ed into a list[str], which the OTEL exporter then serialized again on export, producing escaped JSON:

"[\"{\\\"role\\\": \\\"user\"\\, ...

A minor related issue: gen_ai.tool.call.result was unconditionally json.dumps()-ed without checking if the value was already a string.

Fix

Per the OTEL GenAI semantic conventions, these attributes are typed any and should be recorded as a single JSON string when structured attributes are not supported on spans (which is the case for the Python OTEL SDK today).

  • Formatting helpers (serialize_* -> format_*) now return dict instead of str
  • Callers in otel.py collect into list[dict], then do a single json.dumps() per attribute
  • gen_ai.tool.call.result now has an isinstance(str) guard matching tool.call.arguments
  • Renamed serialization.py -> formatting.py to reflect that these functions format, not serialize

References

@simonvdk-mistral simonvdk-mistral marked this pull request as ready for review April 17, 2026 17:06
Copy link
Copy Markdown
Contributor

@louis-sanna-dev louis-sanna-dev left a comment

Choose a reason for hiding this comment

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

🔭

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.

2 participants