Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

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:

  • GPT-5 wraps tool arguments in an array: [{"arg": "value"}, []]
  • GPT-4 uses a flat dict: {"arg": "value"}
  • CrewAI's tool input validation only accepted dict format, causing GPT-5 calls to fail

The Solution:
Added a _coerce_args_dict() helper method in ToolUsage class that normalizes both formats:

  • Returns dict unchanged (GPT-4 format)
  • Unwraps array when first element is dict and trailing elements are empty (GPT-5 format)
  • Returns None for invalid 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:

  • GPT-5 array-wrapped format with trailing empty array
  • GPT-5 format with single dict element
  • GPT-4 flat dict format (backward compatibility)
  • Complex nested arguments with both formats
  • Invalid list formats (properly rejected)
  • Multiple trailing empty elements

All 71 tests in the tools module pass.

Review & Testing Checklist for Human

  • Review the normalization logic in _coerce_args_dict() (lines 529-550 in tool_usage.py) - specifically the all(not x for x in trailing_elements) check. Does this correctly identify GPT-5's format without being too permissive?
  • Test with actual GPT-5 model - The unit tests mock the input, but we should verify this works end-to-end with a real GPT-5 API call to ensure the format matches what GPT-5 actually outputs
  • Review uv.lock changes - The PR includes a regenerated lock file with dependency updates (necessary to fix a corrupted lock file). Verify these updates are acceptable or if we should revert to the original lock file

Notes

GPT-5 wraps tool arguments in an array format like:
[{"arg": "value"}, []]

while GPT-4 uses a flat dict format:
{"arg": "value"}

This commit adds a _coerce_args_dict() helper method that normalizes
both formats by unwrapping the array when the first element is a dict
and all trailing elements are empty.

The fix is applied across all parsing attempts (JSON, Python literal,
JSON5, and repaired JSON) to ensure consistent behavior.

Tests added to verify:
- GPT-5 array-wrapped format is correctly normalized
- GPT-4 flat dict format continues to work unchanged
- Invalid list formats are properly rejected
- Complex nested arguments work with both formats

Fixes #3889

Co-Authored-By: João <[email protected]>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@Mukhsin0508
Copy link

Did you just do this after I requested the new feature? if so, it was too fast!

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.

2 participants