KEMBAR78
Fix OpenAI UnprocessableEntityError when AssistantAgent makes multiple tool calls by Copilot · Pull Request #6799 · microsoft/autogen · GitHub
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 11, 2025

Problem

When an AssistantAgent makes multiple tool calls in response to a task, the OpenAI API returns an UnprocessableEntityError(422) with the error message:

'detail': [{'type': 'missing', 'loc': ['body', 'messages', 2, 'content'], 'msg': 'Field required'}]

This occurs because the transformed assistant message contains tool_calls but is missing the required content field.

Root Cause

The issue was in the message transformation pipeline in _message_transform.py. When an AssistantMessage has tool calls but no thought, it uses the tools_assistant_transformer_funcs which only included:

  • _set_tool_calls (adds the tool_calls field)
  • Base transformers (adds role, name validation)

But it was missing a content field setter, resulting in messages like:

{
  "role": "assistant",
  "tool_calls": [...]
  // Missing required "content" field
}

According to OpenAI's API specification, assistant messages with tool calls must include a content field (which can be null).

Solution

Added a new transformer function _set_null_content_for_tool_calls that sets content: null for tool calls without thought, and updated tools_assistant_transformer_funcs to include it.

Before:

tools_assistant_transformer_funcs = base_assistant_transformer_funcs + [_set_tool_calls]
# Result: {"role": "assistant", "tool_calls": [...]}  # Missing content

After:

tools_assistant_transformer_funcs = base_assistant_transformer_funcs + [
    _set_tool_calls,
    _set_null_content_for_tool_calls,
]
# Result: {"role": "assistant", "tool_calls": [...], "content": null}  # Complete

Testing

Added comprehensive test coverage for both scenarios:

  • AssistantMessage with tool calls but no thought → content: null
  • AssistantMessage with tool calls and thought → content: thought_text

Impact

  • Fixes the exact issue: No more OpenAI UnprocessableEntityError(422) for multiple tool calls
  • Minimal and surgical: Only 7 lines added to core code, no existing functionality modified
  • Backward compatible: All existing scenarios continue to work unchanged
  • API compliant: Follows OpenAI specification requirements exactly

Fixes #6788.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…nt field is always present

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI changed the title [WIP] OpenAI.UnprocessableEntityError(422) when AssistantAgent invokes multiple tool calls Fix OpenAI UnprocessableEntityError when AssistantAgent makes multiple tool calls Jul 11, 2025
Copilot AI requested a review from ekzhu July 11, 2025 11:56
Copilot finished work on behalf of ekzhu July 11, 2025 11:56
…ting

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI requested a review from ekzhu July 17, 2025 18:14
Copilot finished work on behalf of ekzhu July 17, 2025 18:14
@codecov
Copy link

codecov bot commented Jul 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.92%. Comparing base (ae024e2) to head (90275c6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6799      +/-   ##
==========================================
+ Coverage   79.87%   79.92%   +0.05%     
==========================================
  Files         233      233              
  Lines       18105    18108       +3     
==========================================
+ Hits        14461    14473      +12     
+ Misses       3644     3635       -9     
Flag Coverage Δ
unittests 79.92% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu marked this pull request as ready for review July 20, 2025 07:21
@ekzhu ekzhu merged commit 7c536a8 into main Jul 20, 2025
70 checks passed
@ekzhu ekzhu deleted the copilot/fix-6788 branch July 20, 2025 07:30
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.

OpenAI.UnprocessableEntityError(422) when AssistantAgent invokes multiple tool calls

2 participants