fix(react): leave list[str] items uncoerced when a schema is provided#953
Draft
cotovanu-cristian wants to merge 1 commit into
Draft
fix(react): leave list[str] items uncoerced when a schema is provided#953cotovanu-cristian wants to merge 1 commit into
cotovanu-cristian wants to merge 1 commit into
Conversation
coerce_json_strings protects scalar str-typed fields from coercion when a
schema is supplied, but the list branch of _coerce_field never extended that
protection to list[str] item types: each element was passed to blind
coercion, so a string element that merely looks like JSON ('{"a": 1}',
'[1, 2]') was silently parsed into a dict/list against the declared str type.
Guard the list branch the same way the scalar branch is guarded: when the
(optional-unwrapped) item type is str, leave the list elements untouched.
Model-typed and unknown item types are unchanged.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
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
When
coerce_json_stringsis given a schema, it protects scalarstr-typed fields from coercion(
if annotation is str: return value). But_coerce_field's list branch never extended thatprotection to
list[str]item types: each element was passed to blind coercion, so alist[str]element that merely looks like JSON (
'{"a": 1}','[1, 2]') was silently parsed into adict/list — against the declared
strelement type.The contract
coerce_json_strings' docstring: "When a schema is provided, str-typed fields are leftuntouched." The elements of a
list[str]field arestr-typed per the schema and must be leftuntouched, exactly like a scalar
strfield. The scalar-field form of this rule is already lockedby
test_str_field_preserved_dict_field_coerced.Root cause & fix
agent/react/json_utils.py::_coerce_field— the list branch now mirrors the scalar guard: when the(optional-unwrapped) item type is
str, the list elements are returned untouched. Model-typed andunknown item types are unchanged (model items still recurse with their child schema). One guard in
the shared producer every
coerce_json_strings(dict, schema)field routes through — not aper-caller patch.
Tests
Added
TestCoerceListOfStrProtected:list[str]items not coerced,Optional[list[str]]itemsnot coerced, and a regression guard that model-typed list items are still coerced. The first
two fail on current code and pass with the fix; the full
json_utilssuite passes.