Skip to content

fix(watsonx): wrap string embedding input in array for WatsonX API#30897

Merged
mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_wtsonx_embedding_string
Jun 20, 2026
Merged

fix(watsonx): wrap string embedding input in array for WatsonX API#30897
mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_wtsonx_embedding_string

Conversation

@shivamrawat1

Copy link
Copy Markdown
Collaborator

WatsonX embedding requests fail with 400 invalid_request_entity when input is sent as a single string (valid OpenAI format)

Issue
Calling /v1/embeddings on the proxy with a WatsonX model (e.g. multilingual-e5-large backed by watsonx/intfloat/multilingual-e5-large) and a string input returns:

WatsonxException - Failed to deserialize json: 'Mismatch type []string with value string'
The upstream payload shows "inputs":"cdsc" instead of "inputs":["cdsc"]. OpenAI clients and the LiteLLM UI commonly send "input": "cdsc" for single-text embeddings, so this breaks the normal embedding path for WatsonX.

Root cause
IBMWatsonXEmbeddingConfig.transform_embedding_request passed the OpenAI input value straight through as WatsonX inputs. OpenAI accepts both a string and a list of strings; WatsonX /ml/v1/text/embeddings always expects inputs to be []string. A string input was serialized as a JSON string, which WatsonX rejected.

Fix
Normalize input before building the WatsonX request body: wrap a single string in a one-element list, pass through an existing list of strings unchanged, and raise a clear error for token-array inputs (which WatsonX does not support).

Added regression tests in tests/test_litellm/llms/watsonx/embed/test_watsonx_embedding_transformation.py covering string input, list input, and token-array rejection.

Screenshots / Proof of Fix
Before (broken payload sent to WatsonX):

{"inputs":"cdsc","parameters":{...},"model_id":"intfloat/multilingual-e5-large","project_id":"..."}
After fix, unit test + mocked integration confirm the payload is:

{"inputs":["cdsc"],"parameters":{...},"model_id":"intfloat/multilingual-e5-large","project_id":"..."}
Live proxy repro (restart proxy after pulling this branch):

uv run litellm --config litellm/proxy/test.yaml --port 4000 --detailed_debug --reload
curl -s http://localhost:4000/v1/embeddings \
  -H "Authorization: Bearer sk-1234" \
  -H "Content-Type: application/json" \
  -d '{"model":"multilingual-e5-large","input":"cdsc"}'

Type
Bug Fix

Changes
litellm/llms/watsonx/embed/transformation.py: wrap string input into inputs: [str] before calling WatsonX
tests/test_litellm/llms/watsonx/embed/test_watsonx_embedding_transformation.py: regression tests for string, list, and token-a

WatsonX text/embeddings expects inputs as []string; OpenAI clients often send a single string.

Co-authored-by: Cursor <[email protected]>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Shivam Rawat seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a WatsonX embedding failure where a single-string input was forwarded directly to the WatsonX API as a JSON string, causing a 400 Mismatch type []string with value string error. The fix normalises the input in IBMWatsonXEmbeddingConfig.transform_embedding_request before building the request body, and a new test file validates the three key cases.

  • String input is now wrapped in a one-element list ("cdsc"["cdsc"]), matching the WatsonX []string requirement.
  • List-of-strings input is passed through unchanged, preserving existing batch-embedding behaviour.
  • Token-array inputs (List[int] / List[List[int]]) raise a clear ValueError, since WatsonX does not support token-based embeddings; three unit tests (no network calls) cover all branches.

Confidence Score: 5/5

Safe to merge — the change is a one-function normalisation with no effect on other code paths.

The fix is surgical: a 10-line normalisation block added before the return statement of a single method, with no changes to surrounding logic, auth handling, or response parsing. The three new unit tests directly exercise the added branches and confirm the before/after payload shape. No existing tests were modified, no production code outside the WatsonX embedding transformation was touched.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/watsonx/embed/transformation.py Adds input normalization in transform_embedding_request: wraps string inputs in a list and rejects token-array inputs (List[int] / List[List[int]]) that WatsonX does not support. Change is minimal and correct.
tests/test_litellm/llms/watsonx/embed/test_watsonx_embedding_transformation.py New test file covering string-wrapping, list pass-through, and token-array rejection. All tests are pure unit tests with no network calls, satisfying the mock-only constraint for this folder.

Reviews (2): Last reviewed commit: "fix(watsonx): avoid UP006 in embed trans..." | Re-trigger Greptile

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/llms/watsonx/embed/transformation.py 85.71% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

Shivam Rawat and others added 2 commits June 20, 2026 15:24
Use list[str] and branch-based input normalization instead of List and cast
so the watsonx embedding change does not add strict ruff UP006 violations.

Co-authored-by: Cursor <[email protected]>
@shivamrawat1

Copy link
Copy Markdown
Collaborator Author

@greptile review again with all commits as we resolved linting issues

@mateo-berri mateo-berri enabled auto-merge (squash) June 20, 2026 22:50

@mateo-berri mateo-berri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM; thanks!

@mateo-berri mateo-berri merged commit c7efa77 into litellm_internal_staging Jun 20, 2026
116 of 120 checks passed
@mateo-berri mateo-berri deleted the litellm_wtsonx_embedding_string branch June 20, 2026 22:50
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…erriAI#30897)

* fix(watsonx): wrap string embedding input in array for WatsonX API

WatsonX text/embeddings expects inputs as []string; OpenAI clients often send a single string.

Co-authored-by: Cursor <[email protected]>

* style(watsonx): format watsonx embed transformation for black

Co-authored-by: Cursor <[email protected]>

* fix(watsonx): avoid UP006 in embed transformation strict lint gate

Use list[str] and branch-based input normalization instead of List and cast
so the watsonx embedding change does not add strict ruff UP006 violations.

Co-authored-by: Cursor <[email protected]>

---------

Co-authored-by: Shivam Rawat <[email protected]>
Co-authored-by: Cursor <[email protected]>
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