fix: reject non-dict items in the messages array with a clean 400 - #152
Open
shrdgn wants to merge 1 commit into
Open
fix: reject non-dict items in the messages array with a clean 400#152shrdgn wants to merge 1 commit into
shrdgn wants to merge 1 commit into
Conversation
server.py validated that messages is a non-empty list but not that each item is a dict, unlike router.py/cache.py/estimate.py which all guard with isinstance(message, dict). A malformed item (e.g. a bare string) reached panel.py/pipeline.py/ranked.py/synthesize.py, which call message.get(...) without guarding and crash with AttributeError -- caught by the endpoint's catch-all and surfaced as a 502 UpstreamError instead of a proper 400 InvalidRequestError.
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.
What & why
chat_completionsinopenfusion/server.pyvalidated thatmessagesis a non-empty list, but never checked that each item in it is an object. That check exists inconsistently elsewhere in the codebase:router.py,cache.py, andestimate.pyall guard message access withisinstance(message, dict), butpanel.py,pipeline.py,ranked.py, andsynthesize.pycallmessage.get(...)directly on every item with no such guard.A request like:
{"model": "openfusion", "messages": [{"role": "user", "content": "hi"}, "oops"]}reaches those unguarded call sites and raises an
AttributeError: 'str' object has no attribute 'get'. The endpoint's catch-allexcept Exceptionturns that into a genericUpstreamError, which defaults to HTTP 502 — the client gets an opaque "upstream" failure for what is really a malformed request that should be a clean 400invalid_request_error, consistent with the existingmessages is required and must be a non-empty arraycheck right above it.This PR adds the missing
isinstance(message, dict)check next to the existing list/non-empty check, so malformed items are rejected at the request boundary before reaching any fusion strategy.How it was tested
ruff check .passespytest -qpasses (491 passed, no live network)test_rejects_non_dict_message_itemintests/test_server_pass_through.py)bench/run.pynumber — N/A, not a quality/cost changeNotes for reviewers
Found while auditing the codebase for inconsistent input validation; scoped to just the one missing guard rather than touching
panel.py/pipeline.py/ranked.py/synthesize.py, since validating at the API boundary makes the per-module guards unnecessary defense-in-depth rather than required fixes.Generated by Claude Code