- Notifications
You must be signed in to change notification settings - Fork7.2k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
…nt field is always presentCo-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
@pytest.mark.asyncio | ||
async def test_assistant_message_tool_calls_content_field() -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Use other tests as examples and change this test to use a mock client, see if the mock client is invoked with appropriate content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Updated the test to use a mock client pattern following other tests in the file. The test now usesAsyncMock
to mock the OpenAI client'screate
method and verifies that the API call includes the requiredcontent
field properly set. Commit:54e9897
…tingCo-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@## main #6799 +/- ##==========================================- Coverage 80.81% 79.92% -0.90%========================================== Files 233 233 Lines 18074 18108 +34 ==========================================- Hits 14607 14473 -134- Misses 3467 3635 +168
Flags with carried forward coverage won't be shown.Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Uh oh!
There was an error while loading.Please reload this page.
Problem
When an
AssistantAgent
makes multiple tool calls in response to a task, the OpenAI API returns anUnprocessableEntityError(422)
with the error message:This occurs because the transformed assistant message contains
tool_calls
but is missing the requiredcontent
field.Root Cause
The issue was in the message transformation pipeline in
_message_transform.py
. When anAssistantMessage
has tool calls but no thought, it uses thetools_assistant_transformer_funcs
which only included:_set_tool_calls
(adds the tool_calls field)But it was missing a content field setter, resulting in messages like:
According to OpenAI's API specification, assistant messages with tool calls must include a
content
field (which can benull
).Solution
Added a new transformer function
_set_null_content_for_tool_calls
that setscontent: null
for tool calls without thought, and updatedtools_assistant_transformer_funcs
to include it.Before:
After:
Testing
Added comprehensive test coverage for both scenarios:
content: null
content: thought_text
Impact
Fixes#6788.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn moreCopilot coding agent tips in the docs.