fix: validate JSON before Pydantic conversion to avoid false-positive matches on GraphQL schemas#5487
Open
balgaly wants to merge 1 commit intocrewAIInc:mainfrom
Open
Conversation
… matches
_JSON_PATTERN matches any {.*} substring including non-JSON content like
GraphQL schemas. Passing non-JSON to model_validate_json raises a
ValidationError that incorrectly surfaces as a conversion failure.
Fix: call json.loads() on the regex match first. If it raises
JSONDecodeError, the content is not JSON and we fall through to
convert_with_instructions instead of attempting Pydantic validation.
Fixes crewAIInc#5460
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
Fixes #5460
_JSON_PATTERNmatches any{.*}substring, including non-JSON content like GraphQL schemas. When such content is passed tomodel_validate_json, Pydantic raises aValidationErrorthat surfaces as a conversion failure rather than a graceful fallback.Root cause: The regex matches curly-brace content but does not verify it is valid JSON before attempting Pydantic validation.
Fix: Call
json.loads()on the regex match first. If it raisesJSONDecodeError, the content is not JSON and we fall through toconvert_with_instructionsinstead of attempting Pydantic validation.Changes
src/crewai/utilities/converter.py: Addedjson.loads()guard beforemodel_validate_jsoninhandle_partial_jsontests/utilities/test_converter.py: Addedtest_handle_partial_json_with_graphql_schema_does_not_raiseto cover the false-positive caseTest plan
test_handle_partial_json_with_graphql_schema_does_not_raisepassesoutput_pydanticand a GraphQL schema as agent output no longer raisesValidationError