Repository navigation
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the change preserving assistant text alongside serialized tool calls.
No open issues 🎉
What changed in this PR
Preserves assistant text when ChatMessageContent.to_dict() serializes messages containing tool calls, so OpenAI-compatible connectors retain that text in conversation history.
Changes:
- Includes joined text alongside
tool_calls, while preserving the existing payload for messages without text. - Adds a regression case for an assistant message containing both text and a function call.
| File | Description |
|---|---|
python/tests/unit/contents/test_chat_message_content.py |
Adds the mixed text-and-tool-call serialization test case. |
python/semantic_kernel/contents/chat_message_content.py |
Retains assistant text alongside serialized tool calls. |
Review effort: Balanced
Muhammad Abdullah Butt (1AbdullahButt)
left a comment
There was a problem hiding this comment.
I ran ChatMessageContent(role=ASSISTANT, items=...).to_dict() and OpenAIChatCompletion._prepare_chat_history_for_request on main and on this branch.
| items | main content |
this PR content |
|---|---|---|
| text + call | absent | 'Let me check.' |
| call only | absent | absent |
| text + 2 calls | absent | 'Two.' |
| call, then text | absent | 'After.' |
| empty text + call | absent | absent |
whitespace text ' ' + call |
absent | ' ' |
two texts ('First.', 'Second.') + call |
absent | 'First.Second.' |
streamed ('Let me ' + 'check.' + call, merged with +) |
absent | 'Let me check.' |
The OpenAI request message now has both content and tool_calls, as the API expects. tests/unit/contents/test_chat_message_content.py gives 47 passed.
Two small things:
- Several text items are joined with no separator.
'First.'and'Second.'become'First.Second.'. Streamed chunks need""as the separator, and they are already merged by__add__beforeto_dict. So separateTextContentitems here are usually separate blocks, for example from a model that returns text before and after a tool call, and joining them with"\n"(or" ") would keep them readable. Without tool calls, the same message goes through_parse_items, which returns a list of parts instead of a joined string, so the two paths now behave differently. - Whitespace-only text is sent.
if text_content:keeps' '.if text_content.strip():would skip it, the same as empty text.
(I made this review with help from Claude Code. I ran the table above on main and on this branch, and tests/unit/contents/test_chat_message_content.py on this branch. I did not call a real OpenAI endpoint or check other connectors that build their own assistant messages.)
Problem
ChatMessageContent.to_dict()serializes onlytool_callsfor an assistant message that contains anyFunctionCallContent:The text of that turn is still exposed by
ChatMessageContent.contentandto_prompt(), and OpenAI-compatible APIs acceptcontenttogether withtool_calls. The OpenAI/Azure OpenAI connectors build their request payload fromto_dict(), so the model no longer sees what it said in the turn that called a tool.Fix
Include the joined text content alongside
tool_callswhen the message has any; messages without text keep the previous payload exactly.Tests
tests/unit/contents/test_chat_message_content.py(new parametrized casefunction_call_with_text; existing cases unchanged):