Skip to content

fix(llmobs): filter openai Omit/NotGiven sentinels from span metadata [backport 4.11]#18604

Merged
Yun-Kim merged 2 commits into
4.11from
backport-18552-to-4.11
Jun 12, 2026
Merged

fix(llmobs): filter openai Omit/NotGiven sentinels from span metadata [backport 4.11]#18604
Yun-Kim merged 2 commits into
4.11from
backport-18552-to-4.11

Conversation

@dd-octo-sts

@dd-octo-sts dd-octo-sts Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Backport #18552 to 4.11

…#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>
@dd-octo-sts dd-octo-sts Bot requested review from a team as code owners June 12, 2026 16:51
@dd-octo-sts dd-octo-sts Bot requested review from ZStriker19 and gyuheon0h and removed request for a team June 12, 2026 16:51
@cit-pr-commenter-54b7da

Copy link
Copy Markdown

Codeowners resolved as

ddtrace/llmobs/_integrations/utils.py                                   @DataDog/ml-observability
releasenotes/notes/llmobs-filter-openai-omit-metadata-15533386303440c7.yaml  @DataDog/apm-python
tests/contrib/openai/test_openai_llmobs.py                              @DataDog/ml-observability

@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Pipelines  Tests

Fix all issues with BitsAI

⚠️ Warnings

🚦 8 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741238-d2b8243-manylinux2014_x86_64, 1]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux: [amd64, cp315-cp315, v113741491-d2b8243-musllinux_1_2_x86_64]   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-py | build linux serverless: [amd64, cp315-cp315, v113741491-d2b8243-musllinux_1_2_x86_64, 1]   View in Datadog   GitLab

View all 8 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

🔄 Datadog auto-retried 25 jobs - 25 passed on retry View in Datadog

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 04e9f3e | Docs | Datadog PR Page | Give us feedback!

@Yun-Kim Yun-Kim enabled auto-merge (squash) June 12, 2026 19:39
@Yun-Kim Yun-Kim merged commit b3859d9 into 4.11 Jun 12, 2026
592 checks passed
@Yun-Kim Yun-Kim deleted the backport-18552-to-4.11 branch June 12, 2026 19:48
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.

3 participants