Skip to content

Fix OpenAI UnprocessableEntityError when AssistantAgent makes multiple tool calls #6799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot 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 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 Copilot AI requested a review from ekzhu July 11, 2025 11:56
Copilot finished work on behalf of ekzhu July 11, 2025 11:56
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