Fix pre_parse_json corrupting str | None values that look like JSON#3056
Open
vientooscuro wants to merge 1 commit into
Open
Fix pre_parse_json corrupting str | None values that look like JSON#3056vientooscuro wants to merge 1 commit into
vientooscuro wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
No issues found across 2 files
Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more
Re-trigger cubic
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.
Summary
FuncMetadata.pre_parse_json()decided whether tojson.loads()a stringargument based on
field_info.annotation is not str. That check isTruefor
str | Noneas well (it'sOptional[str], not literallystr), so anyoptional string parameter got the same "maybe this is JSON" treatment as a
list/dictparameter.If a caller passes a valid string that also happens to parse as a JSON
object/array — e.g. a JSON-serialized payload passed as a plain string field
(block-based template body, serialized config, etc.) — the value silently
got replaced with a
dict/listbefore the pydantic argument model wasvalidated, and validation then failed with
Input should be a valid stringeven though the caller's string was perfectly valid for that field.
Fixes #3055.
Fix
Added
_is_optional_str(), which recognizesstr | None(any union whoseonly non-
Nonemember isstr) and skips JSON pre-parsing for it, since nomember of such a union other than
strcould ever be produced by decodingJSON — so pre-parsing that annotation can only corrupt, never help.
str | list[str](and other unions with a real non-strmember) keep theexisting behavior, since a JSON array/object there is meaningfully different
from the raw string and should still be pre-parsed — covered by the existing
test_str_vs_list_strtest, which still passes.Test plan
test_optional_str_is_never_json_pre_parsedcovering dict-like,list-like, plain-string, and
Nonevalues for astr | Noneparam.uv run pytest tests/server/mcpserver/test_func_metadata.py— 43 passeduv run pytest tests/server/mcpserver(full suite) — 526 passeduv run ruff check/ruff format --checkon changed files