fix: accept OpenAI-compatible chat completions request shape - #23
Merged
aviweit merged 2 commits intoJul 23, 2026
Merged
Conversation
- ChatMessage.content: str -> Optional[Union[str, List[ContentBlock]]]
Clients like OpenHands send content as a list of typed blocks
({type, text}) for system/user messages and null for assistant
tool-call messages. Both were rejected with 422 before this fix.
- Add ContentBlock model to parse {type, text} blocks; _content_as_str()
flattens list content to a plain string for LangChain consumption.
- Add model_config extra='ignore' to ChatMessage and ChatRequest so
unknown OpenAI fields (stream, top_p, stop, frequency_penalty, etc.)
are silently dropped instead of causing a 422.
Fully backward compatible: existing callers sending plain strings are
unaffected (str branch of the Union is tried first by Pydantic).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: accept OpenAI-compatible chat completions request shape
Problem
POST /chat/completionsreturned422 Unprocessable Entityfor anyclient following the full OpenAI chat completions spec, including
OpenHands. Two distinct validation failures were occurring:
contentas a list of content blocks — OpenAI spec allowscontentto be[{"type": "text", "text": "..."}](multimodalformat).
ChatMessage.content: strrejected this withInput should be a valid string.content: nullon assistant tool-call messages — When anassistant message contains
tool_calls, OpenAI setscontent: null.Same rejection as above.
Unknown fields — OpenAI clients send
stream,top_p,stop,frequency_penalty,presence_penalty, etc. Pydantic's defaultstrict mode rejected these with 422.
Changes (
fast_api/api_server.py)ChatMessage.contenttypestrOptional[Union[str, List[ContentBlock]]]content: null""content: [{"type":"text","text":"..."}]ChatMessageChatRequestNew
ContentBlockmodel parses{type, text}blocks. New_content_as_str()method flattens any content shape to a plain stringbefore passing to LangChain.
Backward compatibility
Fully backward compatible. Existing callers sending
contentas a plainstring hit the same
strbranch of theUnionas before — no behaviourchange. The fix only widens what is accepted.
Testing
Verified with
curlagainst the running server:content: null→ 200 ✅content: [{"type":"text","text":"..."}]→ 200 ✅stream,top_p, …) → 200 ✅