Skip to content

Python: fix: keep assistant text content when a message also carries tool calls - #14839

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/tool-call-message-keeps-text
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/tool-call-message-keeps-text

Conversation

@DZH166

@DZH166 DZH166 commented Nov 20, 2026

Copy link
Copy Markdown

Problem

ChatMessageContent.to_dict() serializes only tool_calls for an assistant message that contains any FunctionCallContent:

if role == AUTHOR_ROLE and self.items:
    if any(isinstance(item, FunctionCallContent) for item in self.items):
        return {"role": role, "tool_calls": [...]}      # text dropped

The text of that turn is still exposed by ChatMessageContent.content and to_prompt(), and OpenAI-compatible APIs accept content together with tool_calls. The OpenAI/Azure OpenAI connectors build their request payload from to_dict(), so the model no longer sees what it said in the turn that called a tool.

before: to_dict() -> {'role': 'assistant', 'tool_calls': [...]}          (content missing)
after : {'role': 'assistant', 'content': 'Let me check the weather for you.', 'tool_calls': [...]}

Fix

Include the joined text content alongside tool_calls when the message has any; messages without text keep the previous payload exactly.

Tests

tests/unit/contents/test_chat_message_content.py (new parametrized case function_call_with_text; existing cases unchanged):

pytest tests/unit/services/test_ai_service_selector.py tests/unit/contents/test_chat_message_content.py -q
# before: 2 failed, 53 passed   after: 55 passed

@DZH166
DZH166 requested a review from a team as a code owner November 20, 2026 16:50
Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:50
@DZH166
DZH166 temporarily deployed to github-app-auth November 20, 2026 16:50 — with GitHub Actions Inactive

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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__ before to_dict. So separate TextContent items 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.
  2. 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.)

This branch was previously deployed

1 inactive deployment
github-app-auth — 0b4af286 Deployed Nov 20, 2026 by DZH166 via add_label #29766
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.

3 participants