Skip to content

fix: validate JSON before Pydantic conversion to avoid false-positive matches on GraphQL schemas#5487

Open
balgaly wants to merge 1 commit intocrewAIInc:mainfrom
balgaly:fix/json-pattern-false-positive-graphql
Open

fix: validate JSON before Pydantic conversion to avoid false-positive matches on GraphQL schemas#5487
balgaly wants to merge 1 commit intocrewAIInc:mainfrom
balgaly:fix/json-pattern-false-positive-graphql

Conversation

@balgaly
Copy link
Copy Markdown

@balgaly balgaly commented Apr 16, 2026

Summary

Fixes #5460

_JSON_PATTERN matches any {.*} substring, including non-JSON content like GraphQL schemas. When such content is passed to model_validate_json, Pydantic raises a ValidationError that 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 raises JSONDecodeError, the content is not JSON and we fall through to convert_with_instructions instead of attempting Pydantic validation.

Changes

  • src/crewai/utilities/converter.py: Added json.loads() guard before model_validate_json in handle_partial_json
  • tests/utilities/test_converter.py: Added test_handle_partial_json_with_graphql_schema_does_not_raise to cover the false-positive case

Test plan

  • Existing converter tests pass
  • New test test_handle_partial_json_with_graphql_schema_does_not_raise passes
  • Task with output_pydantic and a GraphQL schema as agent output no longer raises ValidationError

… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] when using Task(..., output_pydantic=MyModel) some JSON substring identification within the data occurs with possible false positive

1 participant