Fix: Handle malformed tool calls from OpenAI using sanitizer
Problem
Previously, when OpenAI returned a malformed tool_calls response — such as when function.arguments was None, an invalid type, or not a JSON string — it would cause the Autogen agent to crash. This commonly occurred during tool call processing, especially when relying on external model behavior that couldn’t be guaranteed to be clean or well-formed.
Solution
This PR introduces a new sanitizer in autogen_core/utils/sanitizer.py that:
- Intercepts and inspects each tool call in the list.
- Skips calls with missing or malformed function.arguments.
- Filters out invalid JSON or ill-structured inputs.
- Ensures that only well-formed, decodable tool calls are passed onward for execution.
This enhances Autogen's fault-tolerance, especially during external API usage and multi-agent orchestration where malformed tool responses can silently derail entire chains.
Testing
A dedicated test (test_sanitizer.py) was added to:
- Simulate OpenAI-style tool call outputs, including edge cases.
- Validate that malformed entries are removed while valid ones remain unaffected.
- Ensure that the sanitizer logic works in isolation, independent of OpenAI API availability.
Notes
- This PR does not modify agent or model logic— the sanitizer operates as a standalone pre-execution guard.
- Other test failures (if any) are unrelated and existed before this change.
Checks
- [ ] I've included any doc changes needed for https://microsoft.github.io/autogen/.
- [x] I've added tests (if relevant) corresponding to the changes introduced in this PR.
- [x] I've made sure all auto checks have passed.
@microsoft-github-policy-service agree
@microsoft-github-policy-service agree
Hi @ekzhu, just following up on this PR — I’ve made the requested changes and confirmed all CLA requirements are now satisfied. Let me know if there's anything else needed from my side. Thanks!
Hi again @ekzhu, just following up in case this PR got buried. Happy to rebase or resolve conflicts if needed — just let me know. Thanks again for reviewing earlier!
Hi @PranjalPragya,
Could you please resolve the merge conflicts with the main branch?
Thanks!
Hi @ekzhu / @tejas-dharani,
I’ve resolved the merge conflicts with the main branch and committed the changes. All CLA requirements are satisfied, and all requested modifications have been applied. Please let me know if anything else is needed from my side for review.
Thanks!