fix(llmobs): filter openai Omit/NotGiven sentinels from span metadata [backport 4.11]#18604
Merged
Conversation
…#18552) ## Overview The OpenAI integration captured the raw chat-completion request `kwargs` as LLM span metadata, including the openai SDK's `Omit` / `NotGiven` sentinel objects used as defaults for unset parameters. These were serialized to noisy repr strings such as `"<openai.Omit object at 0x7f5e35900e90>"` across most of the ~21 metadata keys per span, making the metadata field unqueryable and burying the real parameters. ## Motivation Frameworks like **PydanticAI** forward every chat-completion parameter explicitly, defaulting any the caller didn't set to `openai.omit`. ddtrace snapshots the `kwargs` at the wrapper boundary — upstream of the openai SDK's own sentinel-stripping (`_merge_mappings`) — so the sentinels reach span metadata even though they never reach the provider. The request to OpenAI/Azure was always correct; only the recorded metadata was polluted. ## Change Filter `Omit` / `NotGiven` sentinel values out before building metadata, in both: - `get_metadata_from_kwargs` (chat / completion) - `openai_get_metadata_from_response` (responses API) Sentinel types are resolved **lazily and independently** via a small cached helper. Lazy resolution avoids a circular import while ddtrace is patching openai at import time; independent resolution keeps `NotGiven` filtering working on `openai<2` (which has no `Omit`). On openai-less installs the helper returns an empty tuple and the filter is a no-op. ## Testing - Added regression test `test_chat_completion_filters_openai_sentinel_metadata` — passes a real value (`top_p`) alongside `Omit`/`NotGiven` sentinels and asserts only the real value lands in metadata. Fails without the fix. - Verified across the full openai riot matrix (latest, `<2.0.0`, `~=1.76.2`, `==1.66.0`) — including 1.x, which exercises the `NotGiven`-only path. - Reproduced the customer's exact stack (PydanticAI → Azure OpenAI): metadata drops from 21 keys (~20 sentinels) to only real values. ## Risk Low. The change only removes sentinel placeholder values from metadata; real parameter values are unaffected. Behavior is unchanged for non-openai integrations. Jira: MLOB-7613 Co-authored-by: jessica.gamio <jessica.gamio@datadoghq.com> (cherry picked from commit faf70d1) Co-authored-by: Jessica Gamio <52049720+jessicagamio@users.noreply.github.com>
Codeowners resolved as |
Contributor
|
ZStriker19
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport #18552 to 4.11