fix(tools): automatically serialize list and dictionary tool outputs to JSON strings#6377
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesJSON serialization fallback for dict/list tool outputs
Suggested Reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/crewai/src/crewai/tools/structured_tool.py`:
- Around line 88-92: The fallback warning in StructuredTool handling is now
misleading because the branch in structured_tool.py can return JSON for
dict/list results before ever using str(raw_result). Update the warning message
in the same raw_result handling path to describe the actual behavior and
distinguish the JSON serialization attempt from the str(raw_result) fallback.
Use the relevant structured result conversion logic around the
isinstance(raw_result, (dict, list)) branch so the log accurately reflects what
happens on validation failure.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: d0919d97-a286-4ad7-a566-d0499ee49a55
📒 Files selected for processing (2)
lib/crewai/src/crewai/tools/structured_tool.pylib/crewai/tests/tools/test_structured_tool.py
…ion exception handler
Currently, when a custom tool returns a dictionary or a list,
format_output_for_agentfalls back to returning the Python string representation (e.g.str(raw_result)which outputs invalid JSON with single quotes like{'foo': 'bar'}). This can cause parsing/validation failures downstream when processed by LLMs or JSON output parsers.This PR updates
_format_tool_output_for_agentto automatically serialize dictionary and list outputs into valid JSON strings usingjson.dumps.I've added unit tests verifying that both dictionary and list tool outputs are successfully serialized to JSON strings.
Closes #6267
Summary by CodeRabbit
Bug Fixes
Tests