Python: Fix #2410: Combine text content and tool_calls into single message per OpenAI API spec#2418
Closed
ishanrajsingh wants to merge 5 commits intomicrosoft:mainfrom
Closed
Conversation
…nAI message - Updated _openai_chat_message_parser in both _chat_client.py and _responses_client.py - Now aggregates text content and tool calls into single message dict per OpenAI API spec - Added exception handling for FunctionResultContent - Fixed approval content handling for Responses API - All existing tests pass
| # These need special handling - add them as content | ||
| if "content" not in msg: | ||
| msg["content"] = [] | ||
| msg["content"].append(self._openai_content_parser(content)) |
Member
There was a problem hiding this comment.
this will add a dict based on our class to the list, I don't think that makes sense, could you 1) add test cases with this and 2) add integration tests that have this. This is used with MCP server side tool calling, so the output format should be determined by openai, not by our type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2410
The OpenAI chat client's
_openai_chat_message_parsermethod was incorrectly splitting messages containing both text content and function/tool calls into separate API messages. According to the OpenAI API specification, a single assistant message can and should contain bothcontentandtool_callsfields in the same message object.Problem
When a
ChatMessagecontained both text content and function calls, the parser created separate messages:Incorrect output (before fix):
[
{"role": "assistant", "content": ["I'll help you with that calculation."]},
{"role": "assistant", "tool_calls": [{"id": "call-123", "type": "function", ...}]}
]
text
Solution
Refactored the parser to aggregate all content types into a single message:
Correct output (after fix):
[
{
"role": "assistant",
"content": ["I'll help you with that calculation."],
"tool_calls": [{"id": "call-123", "type": "function", ...}]
}
]
text
Changes Made
Core Changes
_chat_client.py: Refactored_openai_chat_message_parserto build a single message dictionary that accumulates both text content and tool calls_responses_client.py: Applied the same fix with additional handling for Responses API-specific formatsAdditional Improvements
FunctionResultContentto properly format error messages as"Error: {exception}"FunctionApprovalRequestContentandFunctionApprovalResponseContent) to be top-level items in Responses API as per specificationTesting
test_function_result_exception_handlingtest_end_to_end_mcp_approval_flowImpact
API Compatibility
Context Accuracy
Model Behavior
Breaking Changes
None. This fix corrects the API format to match the specification without changing public interfaces.
Related Issues
Closes #2410
Checklist