Raise MessageParseError when the message field is not a dict - #1214
Open
shashvat-singham wants to merge 1 commit into
Open
Raise MessageParseError when the message field is not a dict#1214shashvat-singham wants to merge 1 commit into
shashvat-singham wants to merge 1 commit into
Conversation
parse_message wraps malformed input in MessageParseError -- non-dict
data, a missing type, missing required fields all get the parser's own
error type. But a "message" field that is not a dict escaped as a bare
TypeError from indexing into it:
parse_message({"type": "user", "message": "hi"})
# TypeError: string indices must be integers, not 'str'
Same for the assistant branch. The existing handlers only catch
KeyError, so TypeError/AttributeError from indexing a non-dict fell
through, and a single malformed line from the CLI stream would surface
as an unrelated-looking TypeError instead of the documented parse error.
Catch TypeError/AttributeError alongside KeyError in both branches and
raise MessageParseError with the offending data attached, like every
other malformation.
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.
Problem
parse_messagewraps every malformation in its ownMessageParseError— non-dict input, missingtype, missing required fields. But a"message"field that isn't a dict escapes as a bareTypeError:The
user/assistantbranches catch onlyKeyError, so indexing into a non-dict falls through. Since this parser consumes the CLI's stream output, one malformed line surfaces as an unrelated-lookingTypeErrorwith no message data attached, instead of the documentedMessageParseError(which carriesdatafor debugging). The other branches (result,stream_event,system) happen to be shielded because they use.get()first, which makes the asymmetry easy to trip over.Change
Catch
TypeError/AttributeErroralongside the existingKeyErrorin theuserandassistantbranches and raiseMessageParseError("Malformed <kind> message: ...", data), consistent with every other malformation.Kept as separate
exceptclauses so the existing "Missing required field" wording forKeyErroris unchanged — only the previously-uncaught exceptions get the new "Malformed" wording.Tests
test_parse_non_dict_message_fieldcovers both branches; it fails onmainwith the rawTypeErrorand passes with the change.