Skip to content

fix(responses): stop internal metadata reaching strict upstreams - #36412

Open
Souravrajvi0 wants to merge 4 commits into
BerriAI:mainfrom
Souravrajvi0:bugfix/responses-metadata-leak-2068
Open

Souravrajvi0 wants to merge 4 commits into
BerriAI:mainfrom
Souravrajvi0:bugfix/responses-metadata-leak-2068

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Summary

Fixes #35780

Strict OpenAI-compatible /v1/responses upstreams were rejecting proxy requests with HTTP 400: Unsupported parameter: metadata even when the client did not pass a metadata argument.

Root cause: internal/proxy metadata (including empty {}) was being serialized into the provider-facing metadata request field during optional-param assembly.

Changes

  • Update ResponsesAPIRequestUtils.get_requested_response_api_optional_param() to:
    • Prefer litellm_metadata.requester_metadata (proxy snapshot of caller metadata)
    • Ignore polluted top-level metadata on proxy routes (detected via user_api_key_* keys in litellm_metadata)
    • Drop empty/non-caller metadata instead of forwarding {} upstream
  • Add regression tests covering empty metadata, internal metadata, caller metadata, and requester_metadata routing

Test plan

  • pytest tests/test_litellm/responses/test_responses_utils.py (32 passed)
  • pytest tests/llm_responses_api_testing/test_openai_responses_api.py::test_openai_responses_litellm_router_no_metadata
  • pytest tests/llm_responses_api_testing/test_openai_responses_api.py::test_openai_responses_litellm_router_with_metadata
  • Manual mocked e2e: internal/empty metadata no longer appears in upstream JSON; explicit caller metadata still forwarded

When proxy-internal fields leaked into the provider-facing metadata
parameter (including empty {}), strict OpenAI-compatible /v1/responses
backends returned HTTP 400. Only forward caller metadata: prefer
litellm_metadata.requester_metadata on proxy routes and drop metadata
entirely when no caller-supplied values exist.

Fixes BerriAI#35780
@greptile-apps

greptile-apps Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR changes Responses API optional-parameter assembly to separate caller metadata from LiteLLM proxy metadata and omit empty or internal metadata before provider dispatch.

  • Prefers the proxy's caller-metadata snapshot when available.
  • Detects proxy context through user_api_key_* fields.
  • Adds regression coverage for empty, internal, direct caller, and snapshotted metadata.

Confidence Score: 4/5

The PR should not merge until caller metadata containing a requester_metadata key is preserved without being interpreted as another internal envelope.

The changed helper selection can silently truncate otherwise valid caller metadata, while the additional mutation concern is non-blocking.

Files Needing Attention: litellm/responses/utils.py

Important Files Changed

Filename Overview
litellm/responses/utils.py Reworks metadata source selection and filtering, but double-interprets a caller-owned requester_metadata key and violates the repository's immutability convention.
tests/test_litellm/responses/test_responses_utils.py Adds focused regression tests for the intended metadata-routing cases without weakening existing assertions.

Reviews (1): Last reviewed commit: "fix(responses): stop internal metadata r..." | Re-trigger Greptile


if metadata_source is not None:
from litellm.utils import get_requester_metadata

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Caller metadata is double-unwrapped

When caller metadata contains a dictionary-valued requester_metadata key alongside other valid fields, get_requester_metadata() treats that key as another internal envelope and returns it early, silently dropping the caller's sibling metadata fields from the upstream request.

Comment thread litellm/responses/utils.py Outdated
from litellm.utils import add_openai_metadata
litellm_metadata = params.get("litellm_metadata")
if not isinstance(litellm_metadata, dict):
litellm_metadata = {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Metadata assembly mutates local state

The new source-selection logic repeatedly reassigns local variables and mutates non_default_params through assignment and pop(), contrary to the repository's immutable-construction convention and making the final metadata provenance harder to reason about.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@codecov

codecov Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing Souravrajvi0:bugfix/responses-metadata-leak-2068 (813a271) with litellm_internal_staging (f6b9518)1

Open in CodSpeed

Footnotes

  1. No successful run was found on litellm_internal_staging (9ce96c2) during the generation of this report, so f6b9518 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

Use add_openai_metadata on the resolved metadata source instead of
get_requester_metadata, which treated caller-owned requester_metadata
keys as another proxy envelope and dropped sibling fields.

Build the final optional-params dict without mutating metadata via pop.
Use Final assignments and a single metadata_source expression so LIT001
and LIT010 budgets are not exceeded.
@yuneng-berri
yuneng-berri deleted the branch BerriAI:main September 13, 2026 04:40
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@devin-ai-integration
devin-ai-integration Bot changed the base branch from litellm_internal_staging to main September 23, 2026 15:11
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 23, 2026 15:11

This branch has not been deployed

No deployments
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.

[Bug]: Responses API sends metadata to a strict OpenAI-compatible upstream although the client does not pass metadata

2 participants