fix(auto_router): handle content-block message format in routing (#25370)

When a chat message uses the OpenAI multi-modal content format
(content: [{"type": "text", "text": "..."}]) instead of a plain
string, message_content becomes a list. Passing a list as the
embedding input causes providers that only accept strings or arrays
of strings (e.g. LM Studio, OpenRouter) to return HTTP 400.

Extract text parts from content blocks before passing to the
semantic router so any embedding backend receives a plain string.

Fixes: https://github.com/BerriAI/litellm/issues/19016

Co-authored-by: stelloxx <stelloxx@users.noreply.github.com>
This commit is contained in:
stelloxx 2026-04-08 17:31:47 -04:00 committed by GitHub
parent 2dac54b732
commit 2964761e0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -120,7 +120,15 @@ class AutoRouter(CustomLogger):
)
user_message: Dict[str, str] = messages[-1]
message_content: str = user_message.get("content", "")
message_content: Union[str, list] = user_message.get("content", "")
# Handle content as a list of content blocks (OpenAI multi-modal format).
# Embedding models expect a plain string; extract text parts when needed.
if isinstance(message_content, list):
message_content = " ".join(
block.get("text", "")
for block in message_content
if isinstance(block, dict) and block.get("type") == "text"
)
route_choice: Optional[Union[RouteChoice, List[RouteChoice]]] = self.routelayer(
text=message_content
)