Skip to content

Commit 5810f65

Browse files
Flatten text-only message content to string for all roles
Extend the system/developer fix to all message roles. Text-only content lists are now post-processed into plain strings, while multimodal content (text + images/audio) remains as a list. This fixes compatibility with OpenAI-like endpoints that cannot deserialize list content (e.g. Foundry Local's Neutron backend). Partially fixes microsoft#4084 Co-authored-by: Copilot <[email protected]>
1 parent 2706e1b commit 5810f65

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎python/packages/core/agent_framework/openai/_chat_client.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,15 @@ def _prepare_message_for_openai(self, message: Message) -> list[dict[str, Any]]:
588588
args["content"].append(self._prepare_content_for_openai(content)) # type: ignore
589589
if "content" in args or "tool_calls" in args:
590590
all_messages.append(args)
591+
592+
# Flatten text-only content lists to plain strings for broader
593+
# compatibility with OpenAI-like endpoints (e.g. Foundry Local).
594+
# See https://github.com/microsoft/agent-framework/issues/4084
595+
for msg in all_messages:
596+
content = msg.get("content")
597+
if isinstance(content, list) and all(isinstance(c, dict) and c.get("type") == "text" for c in content):
598+
msg["content"] = "\n".join(c.get("text", "") for c in content)
599+
591600
return all_messages
592601

593602
def _prepare_content_for_openai(self, content: Content) -> dict[str, Any]:

‎python/packages/core/tests/openai/test_openai_chat_client.py‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,8 @@ def test_prepare_message_with_text_reasoning_content(openai_unit_test_env: dict[
642642
assert len(prepared) == 1
643643
assert "reasoning_details" in prepared[0]
644644
assert prepared[0]["reasoning_details"] == mock_reasoning_data
645-
# Should also have the text content
646-
assert prepared[0]["content"][0]["type"] == "text"
647-
assert prepared[0]["content"][0]["text"] == "The answer is 42."
645+
# Should also have the text content (flattened to string for text-only)
646+
assert prepared[0]["content"] == "The answer is 42."
648647

649648

650649
def test_function_approval_content_is_skipped_in_preparation(openai_unit_test_env: dict[str, str]) -> None:
@@ -690,8 +689,7 @@ def test_function_approval_content_is_skipped_in_preparation(openai_unit_test_en
690689
)
691690
prepared_mixed = client._prepare_message_for_openai(mixed_message)
692691
assert len(prepared_mixed) == 1 # Only text content should remain
693-
assert prepared_mixed[0]["content"][0]["type"] == "text"
694-
assert prepared_mixed[0]["content"][0]["text"] == "I need approval for this action."
692+
assert prepared_mixed[0]["content"] == "I need approval for this action."
695693

696694

697695
def test_usage_content_in_streaming_response(openai_unit_test_env: dict[str, str]) -> None:
@@ -903,8 +901,12 @@ def test_prepare_system_message_multiple_text_contents_joined(openai_unit_test_e
903901
assert prepared[0]["content"] == "You are a helpful assistant.\nBe concise."
904902

905903

906-
def test_prepare_user_message_content_remains_list(openai_unit_test_env: dict[str, str]) -> None:
907-
"""Test that user message content remains a list to support multimodal content."""
904+
def test_prepare_user_message_text_content_is_string(openai_unit_test_env: dict[str, str]) -> None:
905+
"""Test that text-only user message content is flattened to a plain string.
906+
907+
Some OpenAI-compatible endpoints (e.g. Foundry Local) cannot deserialize
908+
the list format. See https://github.com/microsoft/agent-framework/issues/4084
909+
"""
908910
client = OpenAIChatClient()
909911

910912
message = Message(role="user", contents=[Content.from_text(text="Hello")])
@@ -913,7 +915,41 @@ def test_prepare_user_message_content_remains_list(openai_unit_test_env: dict[st
913915

914916
assert len(prepared) == 1
915917
assert prepared[0]["role"] == "user"
916-
assert isinstance(prepared[0]["content"], list)
918+
assert isinstance(prepared[0]["content"], str)
919+
assert prepared[0]["content"] == "Hello"
920+
921+
922+
def test_prepare_user_message_multimodal_content_remains_list(openai_unit_test_env: dict[str, str]) -> None:
923+
"""Test that multimodal user message content remains a list."""
924+
client = OpenAIChatClient()
925+
926+
message = Message(
927+
role="user",
928+
contents=[
929+
Content.from_text(text="What's in this image?"),
930+
Content.from_uri(uri="https://example.com/image.png", media_type="image/png"),
931+
],
932+
)
933+
934+
prepared = client._prepare_message_for_openai(message)
935+
936+
# Multimodal content must stay as list for the API
937+
has_list_content = any(isinstance(m.get("content"), list) for m in prepared)
938+
assert has_list_content
939+
940+
941+
def test_prepare_assistant_message_text_content_is_string(openai_unit_test_env: dict[str, str]) -> None:
942+
"""Test that text-only assistant message content is flattened to a plain string."""
943+
client = OpenAIChatClient()
944+
945+
message = Message(role="assistant", contents=[Content.from_text(text="Sure, I can help.")])
946+
947+
prepared = client._prepare_message_for_openai(message)
948+
949+
assert len(prepared) == 1
950+
assert prepared[0]["role"] == "assistant"
951+
assert isinstance(prepared[0]["content"], str)
952+
assert prepared[0]["content"] == "Sure, I can help."
917953

918954

919955
def test_tool_choice_required_with_function_name(openai_unit_test_env: dict[str, str]) -> None:

0 commit comments

Comments
 (0)