Fix GPT-5 tool calling format incompatibility #3890
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.
Fix GPT-5 tool calling format incompatibility
Summary
Fixes issue #3889 where GPT-5's tool calling format causes CrewAI to fail with "Tool input must be a valid dictionary" errors.
The Problem:
[{"arg": "value"}, []]{"arg": "value"}The Solution:
Added a
_coerce_args_dict()helper method inToolUsageclass that normalizes both formats:The normalization is applied across all 4 parsing attempts (JSON, Python literal, JSON5, repaired JSON) to ensure consistent behavior.
Test Coverage:
Added 6 new tests covering:
All 71 tests in the tools module pass.
Review & Testing Checklist for Human
_coerce_args_dict()(lines 529-550 in tool_usage.py) - specifically theall(not x for x in trailing_elements)check. Does this correctly identify GPT-5's format without being too permissive?Notes