Skip to content

fix(embeddings): unwrap extra_body for providers with their own embedding handlers - #51335

Merged
kerry-berri merged 10 commits into
BerriAI:mainfrom
kenneth-berri:litellm_embedding_extra_body_unwrap
Nov 12, 2026
Merged

kerry-berri merged 10 commits into
BerriAI:mainfrom
kenneth-berri:litellm_embedding_extra_body_unwrap

Conversation

@kenneth-berri

@kenneth-berri kenneth-berri commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Handwritten 1-3 sentence description

"extra_body" fields were being dropped when being sent to certain embedders. The fields are now being sent in the provider body.

Details

TLDR

Problem this solves:

  • extra_body on embeddings only worked for providers that call the OpenAI SDK or base_llm_http_handler
  • Bedrock, Gemini, Vertex, Ollama and SageMaker got it as a literal extra_body key, so fields were dropped or the provider 400ed
  • NVIDIA NIM nested the caller's extra_body inside its own and sent both

How it solves it:

  • extra_body_fields / split_extra_body in litellm/llms/base_llm/embedding/transformation.py pull the caller's extra_body out of optional_params
  • Each native handler merges those fields into the provider body after its own transform, the same way base_llm_http_handler.embedding does, so allowlists like Gemini's _filter_embed_params no longer drop them. Bedrock merges into every invoke body, Gemini into every requests[] entry, Vertex text and multimodal into parameters, Ollama and SageMaker into the request body
  • The merge is body level, happens after model_id, aws_*, vertex_project and the other control keys are read from optional_params, and the transformed body wins, so a client body cannot override auth, routing, the input or a mapped param
  • NvidiaNimEmbeddingConfig.map_openai_params merges the nested extra_body instead of nesting it
  • Bedrock signs exactly the body it sends, test_bedrock_embedding_signs_the_body_it_sends replays SigV4 over the wire body and compares the Authorization header
  • One capture test per affected handler, each failing on main

User Flow

Before: a developer asks a Bedrock Titan embedding for 256 dimensions through extra_body and gets a provider error

  1. They send POST https://litellm-domain/v1/embeddings with {"model": "titan-embed-text-v2", "input": "hello world", "extra_body": {"dimensions": 256}}
  2. Bedrock answers 400 Malformed input request: extraneous key [extra_body] is not permitted, surfaced as a BadRequestError
  3. The same request to gemini-embedding-001 with extra_body: {"outputDimensionality": 8} returns 200 but a 3072-float vector, the setting was dropped

After: the same requests honor the extra_body fields

  1. They send the same POST https://litellm-domain/v1/embeddings with extra_body: {"dimensions": 256} to the Titan model
  2. They get 200 and a 256-float vector
  3. The Gemini and Vertex requests with extra_body: {"outputDimensionality": 8} return 8-float vectors

Linear ticket

Resolves LIT-10698

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Shared setup. Two proxies from the same config, one from a worktree at the merge base on port 4001 and one from the branch tip on port 4002, no database and no Redis (the change is request translation only, nothing is shared across workers). Both logged Running 2 workers.

python litellm/proxy/proxy_cli.py --config config.yaml --port <port> --num_workers 2 --detailed_debug --reload --use_v2_migration_resolver 2>&1 | tee litellm.log
config.yaml (env vars GEMINI_API_KEY, VERTEX_AI_DEVIN_SERVICE_ACCOUNT_JSON, LITELLM_MASTER_KEY, AWS profile litellm-dev)
model_list:
  - model_name: gemini-embedding-001
    litellm_params:
      model: gemini/gemini-embedding-001
      api_key: os.environ/GEMINI_API_KEY
  - model_name: vertex-gemini-embedding-001
    litellm_params:
      model: vertex_ai/gemini-embedding-001
      vertex_project: vertex-check-481318
      vertex_location: us-central1
      vertex_credentials: os.environ/VERTEX_AI_DEVIN_SERVICE_ACCOUNT_JSON
  - model_name: vertex-text-embedding-005
    litellm_params:
      model: vertex_ai/text-embedding-005
      vertex_project: vertex-check-481318
      vertex_location: us-central1
      vertex_credentials: os.environ/VERTEX_AI_DEVIN_SERVICE_ACCOUNT_JSON
  - model_name: titan-embed-text-v2
    litellm_params:
      model: bedrock/amazon.titan-embed-text-v2:0
      aws_profile_name: litellm-dev
      aws_region_name: us-east-1
  - model_name: ollama-all-minilm
    litellm_params:
      model: ollama/all-minilm
      api_base: http://127.0.0.1:11434

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY

Client, run once per model with the extra_body shown in the output lines. The script prints the HTTP status and the returned vector length.

curl -s http://127.0.0.1:$PORT/v1/embeddings -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H 'Content-Type: application/json' \
  -d '{"model": "<model>", "input": "hello world", "extra_body": <extra_body>}'

Before (801e139)

  1. Gemini and Vertex ignore the dimension setting, Bedrock rejects the literal key
gemini-embedding-001 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 3072
vertex-gemini-embedding-001 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 3072
vertex-text-embedding-005 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 768
titan-embed-text-v2 extra_body={"dimensions": 256} -> HTTP 400; error: {"error": {"message": "litellm.BadRequestError: BedrockException - {\"message\":\"Malformed input request: extraneous key [extra_body] is not permitted, please reformat your input and try again.\"} ...
ollama-all-minilm extra_body={"truncate": false} (700 token input, 512 token model) -> HTTP 200; vector length 384
ollama-all-minilm extra_body={"truncate": true} (700 token input) -> HTTP 200; vector length 384

The Ollama truncate: false was dropped into options, so Ollama silently truncated the 700 token input.

After (41aa0a1)

  1. Every provider applies the extra_body field
gemini-embedding-001 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 8
vertex-gemini-embedding-001 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 8
vertex-text-embedding-005 extra_body={"outputDimensionality": 8} -> HTTP 200; vector length 8
titan-embed-text-v2 extra_body={"dimensions": 256} -> HTTP 200; vector length 256
ollama-all-minilm extra_body={"truncate": false} (700 token input, 512 token model) -> HTTP 400; error: {"error": {"message": "litellm.BadRequestError: OllamaException - {\"error\":\"the input length exceeds the context length\"} ...
ollama-all-minilm extra_body={"truncate": true} (700 token input) -> HTTP 200; vector length 384

Ollama now receives truncate: false and refuses the oversized input, which is what the caller asked for. Ollama ran locally from the v0.40.16 GitHub release (ollama-linux-amd64.tar.zst, sha256 verified against the release sha256sum.txt) with all-minilm.

SageMaker and NVIDIA NIM have no live endpoint on the QA box, they are covered by the capture tests in tests/unit/llms/sagemaker/test_sagemaker_embedding_voyage.py and tests/unit/llms/nvidia_nim/test_nvidia_nim.py, each of which fails on main.

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • extra_body is the raw provider body, so a Vertex multimodal caller writes {"dimension": 128} and a Bedrock Titan caller {"dimensions": 256}, the OpenAI style dimensions kwarg is still mapped per provider
  • Each native handler now carries a two line merge, the ticket's ideal of one shared unwrap before dispatch does not survive the provider allowlists and the routing key concern from review

Low

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

kenneth-berri and others added 2 commits November 12, 2026 02:01
…ding handlers

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…and keep lint gates flat

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@veria-ai

veria-ai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 2 · PR risk: 0/10

Reviews

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@greptile-apps

greptile-apps Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge, with no new actionable findings.

Summary

This PR sends extra_body fields to native embedding providers after each provider builds its request. NVIDIA NIM now flattens nested fields instead of sending another extra_body key.

  • Bedrock embedding calls send provider fields in each invoke body.
  • Gemini and Vertex embedding calls place provider fields where each API reads them.
  • Ollama and SageMaker embedding calls send provider fields in their request bodies.
  • NVIDIA NIM embedding calls flatten nested provider fields into one request body.
  • Embedding parameter cleanup leaves request behavior unchanged.

Reviews (5) · Last reviewed commit: "fix(embeddings): keep the shared handler..." · Reviewed by Greptile

Comment thread litellm/utils.py Resolved
Comment thread litellm/utils.py Resolved
Comment thread litellm/utils.py Resolved

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

Comment thread litellm/utils.py Resolved
@codspeed

codspeed Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing kenneth-berri:litellm_embedding_extra_body_unwrap (49d568c) with main (d723545)

Open in CodSpeed

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/utils.py 33.33% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

kenneth-berri and others added 4 commits November 12, 2026 03:05
…nused merge helper

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ative embedding transform

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Comment thread litellm/llms/bedrock/embed/embedding.py Resolved
kenneth-berri and others added 2 commits November 12, 2026 06:19
…t and mapped params cannot be replaced

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

Comment thread litellm/llms/bedrock/embed/embedding.py Resolved

@cursor cursor Bot left a comment •

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.

Stale Bugbot comment from a previous run.

Comment thread litellm/llms/bedrock/embed/embedding.py Resolved
@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptileai

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor cursor Bot left a comment

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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1546031. Configure here.

@greptile-apps

greptile-apps Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

kenneth-berri and others added 2 commits November 12, 2026 18:35
…red handler so extra_body never replaces the input

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… router batch limit relies on extra_body.input

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptileai

1 similar comment
@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptileai

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.

2 participants