Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(passthrough): walk scalar request bodies through managed-id rewrite again

mateo-berri8b874263e2aa
diff --git a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py
index 063ac1a9273..72ca942fe21 100644
--- a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py
+++ b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py
@@ -1168,13 +1168,23 @@ async def rewrite_body_ids(
) -> list[object]: ...
+@overload
+async def rewrite_body_ids(
+ body: object,
+ provider: str,
+ user_api_key_dict: UserAPIKeyAuth,
+ prisma_client: PrismaClient | None,
+ managed_files_hook: CustomLogger | None,
+) -> object: ...
+
+
async def rewrite_body_ids(
- body: dict[str, object] | list[object] | None,
+ body: object,
provider: str,
user_api_key_dict: UserAPIKeyAuth,
prisma_client: PrismaClient | None,
managed_files_hook: CustomLogger | None,
-) -> dict[str, object] | list[object] | None:
+) -> object:
"""
Recursively walk a request body dict/list and resolve any passthrough
managed IDs. Skips litellm internal keys (``litellm_*``).
@@ -1219,7 +1229,7 @@ async def rewrite_body_ids(
return node
return node
- rewritten = await _walk_sequence(body, 0) if isinstance(body, list) else await _walk_mapping(body, 0)
+ rewritten = await _walk(body, 0)
if rewritten is not body:
verbose_proxy_logger.debug("managed_id_rewriter: body ids rewritten provider=%s", provider)
return rewritten
diff --git a/tests/pass_through_unit_tests/test_passthrough_managed_ids.py b/tests/pass_through_unit_tests/test_passthrough_managed_ids.py
index e1a2bc0fe2b..cbbf9257118 100644
--- a/tests/pass_through_unit_tests/test_passthrough_managed_ids.py
+++ b/tests/pass_through_unit_tests/test_passthrough_managed_ids.py
@@ -1274,6 +1274,31 @@ class TestRewriteBodyIds:
assert result is not body
assert result == [{"input_file_id": "file-top-level"}, "raw-string"]
+ @pytest.mark.asyncio
+ async def test_scalar_body_passes_through_unchanged(self):
+ """A truthy scalar JSON body (bare string/number/bool) must pass through
+ unchanged instead of raising while walking a non-container body."""
+ hook = _managed_files_hook()
+
+ for body in ("plain-string-body", 42, 3.14, True):
+ result = await rewrite_body_ids(body, "openai", _user(), None, hook)
+ assert result is body
+
+ @pytest.mark.asyncio
+ async def test_top_level_managed_id_string_body_resolved(self):
+ """A bare managed-ID string body is resolved to the raw provider ID,
+ matching how the same string is resolved when nested in a dict."""
+ mid = encode("openai", "u", "file-scalar")
+ hook = _managed_files_hook()
+ file_row = MagicMock()
+ file_row.created_by = "user-1"
+ file_row.team_id = "team-1"
+ hook.get_unified_file_id = AsyncMock(return_value=file_row)
+
+ result = await rewrite_body_ids(mid, "openai", _user(), None, hook)
+
+ assert result == "file-scalar"
+
@pytest.mark.asyncio
async def test_forged_managed_id_raises_404(self):
"""An unknown managed ID in the body raises 404 (not passed to upstream)."""