mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-14 23:21:35 +00:00
fix: respect drop_params when mapping metadata.user_id to user in Responses adapter
When litellm.drop_params is True, the Anthropic→Responses API adapter was still unconditionally setting responses_kwargs["user"] from metadata.user_id, causing 400 errors on strict gateways that reject the user field. Guard the assignment behind a drop_params check so the field is omitted when drop_params is enabled. Fixes #26241
This commit is contained in:
parent
b8f7d61400
commit
3bd76e2aa0
2 changed files with 52 additions and 2 deletions
|
|
@ -8,6 +8,8 @@ path used for OpenAI and Azure models.
|
|||
import json
|
||||
from typing import Any, Dict, List, Optional, Union, cast
|
||||
|
||||
import litellm
|
||||
|
||||
from litellm.llms.anthropic.experimental_pass_through.utils import (
|
||||
is_reasoning_auto_summary_enabled,
|
||||
)
|
||||
|
|
@ -384,7 +386,8 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
|
|||
# metadata user_id -> user
|
||||
metadata = anthropic_request.get("metadata")
|
||||
if isinstance(metadata, dict) and "user_id" in metadata:
|
||||
responses_kwargs["user"] = str(metadata["user_id"])[:64]
|
||||
if not litellm.drop_params:
|
||||
responses_kwargs["user"] = str(metadata["user_id"])[:64]
|
||||
|
||||
return responses_kwargs
|
||||
|
||||
|
|
|
|||
|
|
@ -1042,4 +1042,51 @@ class TestTranslateResponse:
|
|||
assert "thinking" in types
|
||||
assert "text" in types
|
||||
assert "tool_use" in types
|
||||
assert result["stop_reason"] == "tool_use"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# drop_params: metadata.user_id -> user field
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDropParamsMetadataUserId:
|
||||
"""litellm.drop_params must suppress the user field mapped from metadata.user_id."""
|
||||
|
||||
def test_user_field_set_when_drop_params_false(self):
|
||||
"""user is included when drop_params is False (default)."""
|
||||
import litellm
|
||||
|
||||
req = _make_request(metadata={"user_id": "alice"})
|
||||
original = litellm.drop_params
|
||||
try:
|
||||
litellm.drop_params = False
|
||||
kwargs = _ADAPTER.translate_request(req)
|
||||
finally:
|
||||
litellm.drop_params = original
|
||||
assert kwargs.get("user") == "alice"
|
||||
|
||||
def test_user_field_omitted_when_drop_params_true(self):
|
||||
"""user is omitted when drop_params is True (issue #26241)."""
|
||||
import litellm
|
||||
|
||||
req = _make_request(metadata={"user_id": "alice"})
|
||||
original = litellm.drop_params
|
||||
try:
|
||||
litellm.drop_params = True
|
||||
kwargs = _ADAPTER.translate_request(req)
|
||||
finally:
|
||||
litellm.drop_params = original
|
||||
assert "user" not in kwargs
|
||||
|
||||
def test_no_metadata_no_user_field(self):
|
||||
"""No metadata means no user field regardless of drop_params."""
|
||||
import litellm
|
||||
|
||||
req = _make_request()
|
||||
original = litellm.drop_params
|
||||
try:
|
||||
litellm.drop_params = False
|
||||
kwargs = _ADAPTER.translate_request(req)
|
||||
finally:
|
||||
litellm.drop_params = original
|
||||
assert "user" not in kwargs
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue