Python: Fix #2410: Combine text content and tool_calls into single message per OpenAI API spec #2418
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