2323
2424from __future__ import annotations
2525
26+ import logging
2627from typing import Literal , cast
2728
2829import pytest
@@ -356,7 +357,9 @@ def test_items_to_messages_with_function_output_item():
356357 assert tool_msg ["content" ] == func_output_item ["output" ]
357358
358359
359- def test_items_to_messages_with_non_text_only_function_output_uses_placeholder_by_default ():
360+ def test_items_to_messages_with_non_text_only_function_output_uses_placeholder_by_default (
361+ caplog : pytest .LogCaptureFixture ,
362+ ):
360363 """Default conversion should keep running without sending an empty tool message."""
361364 func_output_item : FunctionCallOutput = {
362365 "type" : "function_call_output" ,
@@ -369,13 +372,15 @@ def test_items_to_messages_with_non_text_only_function_output_uses_placeholder_b
369372 ],
370373 }
371374
372- messages = Converter .items_to_messages ([func_output_item ])
375+ with caplog .at_level (logging .WARNING , logger = "openai.agents" ):
376+ messages = Converter .items_to_messages ([func_output_item ])
373377
374378 assert len (messages ) == 1
375379 tool_msg = messages [0 ]
376380 assert tool_msg ["role" ] == "tool"
377381 assert tool_msg ["tool_call_id" ] == func_output_item ["call_id" ]
378- assert tool_msg ["content" ] == "[non-text tool output omitted]"
382+ assert tool_msg ["content" ] == "[tool output omitted]"
383+ assert "Replacing the tool output with a placeholder" in caplog .text
379384
380385
381386def test_items_to_messages_with_non_text_only_function_output_raises_in_strict_mode ():
@@ -391,11 +396,46 @@ def test_items_to_messages_with_non_text_only_function_output_raises_in_strict_m
391396 ],
392397 }
393398
394- with pytest .raises (UserError , match = "cannot contain only non-text content" ):
399+ with pytest .raises (UserError , match = "cannot be empty or contain only non-text content" ):
400+ Converter .items_to_messages ([func_output_item ], strict_feature_validation = True )
401+
402+
403+ def test_items_to_messages_with_empty_function_output_uses_placeholder_by_default (
404+ caplog : pytest .LogCaptureFixture ,
405+ ):
406+ """Default conversion should not send an empty tool message."""
407+ func_output_item : FunctionCallOutput = {
408+ "type" : "function_call_output" ,
409+ "call_id" : "somecall" ,
410+ "output" : [],
411+ }
412+
413+ with caplog .at_level (logging .WARNING , logger = "openai.agents" ):
414+ messages = Converter .items_to_messages ([func_output_item ])
415+
416+ assert len (messages ) == 1
417+ tool_msg = messages [0 ]
418+ assert tool_msg ["role" ] == "tool"
419+ assert tool_msg ["tool_call_id" ] == func_output_item ["call_id" ]
420+ assert tool_msg ["content" ] == "[tool output omitted]"
421+ assert "Replacing the tool output with a placeholder" in caplog .text
422+
423+
424+ def test_items_to_messages_with_empty_function_output_raises_in_strict_mode ():
425+ """Strict validation should fail explicitly instead of sending empty output."""
426+ func_output_item : FunctionCallOutput = {
427+ "type" : "function_call_output" ,
428+ "call_id" : "somecall" ,
429+ "output" : [],
430+ }
431+
432+ with pytest .raises (UserError , match = "cannot be empty or contain only non-text content" ):
395433 Converter .items_to_messages ([func_output_item ], strict_feature_validation = True )
396434
397435
398- def test_items_to_messages_with_mixed_function_output_keeps_text_by_default ():
436+ def test_items_to_messages_with_mixed_function_output_keeps_text_by_default (
437+ caplog : pytest .LogCaptureFixture ,
438+ ):
399439 """Default conversion should preserve text parts and omit unsupported non-text parts."""
400440 func_output_item : FunctionCallOutput = {
401441 "type" : "function_call_output" ,
@@ -409,13 +449,15 @@ def test_items_to_messages_with_mixed_function_output_keeps_text_by_default():
409449 ],
410450 }
411451
412- messages = Converter .items_to_messages ([func_output_item ])
452+ with caplog .at_level (logging .WARNING , logger = "openai.agents" ):
453+ messages = Converter .items_to_messages ([func_output_item ])
413454
414455 assert len (messages ) == 1
415456 tool_msg = messages [0 ]
416457 assert tool_msg ["role" ] == "tool"
417458 assert tool_msg ["tool_call_id" ] == func_output_item ["call_id" ]
418459 assert tool_msg ["content" ] == [{"type" : "text" , "text" : "visible text" }]
460+ assert "tool output omitted" not in caplog .text
419461
420462
421463def test_items_to_messages_can_preserve_non_text_function_output () -> None :
0 commit comments