@@ -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
650649def 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
697695def 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.\n Be 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
919955def test_tool_choice_required_with_function_name (openai_unit_test_env : dict [str , str ]) -> None :
0 commit comments