|
23 | 23 |
|
24 | 24 | from __future__ import annotations |
25 | 25 |
|
| 26 | +import logging |
26 | 27 | from typing import Literal, cast |
27 | 28 |
|
28 | 29 | import pytest |
@@ -356,6 +357,139 @@ def test_items_to_messages_with_function_output_item(): |
356 | 357 | assert tool_msg["content"] == func_output_item["output"] |
357 | 358 |
|
358 | 359 |
|
| 360 | +def test_items_to_messages_with_non_text_only_function_output_uses_placeholder_by_default( |
| 361 | + caplog: pytest.LogCaptureFixture, |
| 362 | +): |
| 363 | + """Default conversion should keep running without sending an empty tool message.""" |
| 364 | + func_output_item: FunctionCallOutput = { |
| 365 | + "type": "function_call_output", |
| 366 | + "call_id": "somecall", |
| 367 | + "output": [ |
| 368 | + { |
| 369 | + "type": "input_image", |
| 370 | + "image_url": "https://example.com/image.png", |
| 371 | + } |
| 372 | + ], |
| 373 | + } |
| 374 | + |
| 375 | + with caplog.at_level(logging.WARNING, logger="openai.agents"): |
| 376 | + messages = Converter.items_to_messages([func_output_item]) |
| 377 | + |
| 378 | + assert len(messages) == 1 |
| 379 | + tool_msg = messages[0] |
| 380 | + assert tool_msg["role"] == "tool" |
| 381 | + assert tool_msg["tool_call_id"] == func_output_item["call_id"] |
| 382 | + assert tool_msg["content"] == "[tool output omitted]" |
| 383 | + assert "Replacing the tool output with a placeholder" in caplog.text |
| 384 | + |
| 385 | + |
| 386 | +def test_items_to_messages_with_non_text_only_function_output_raises_in_strict_mode(): |
| 387 | + """Strict validation should fail explicitly instead of silently losing the output.""" |
| 388 | + func_output_item: FunctionCallOutput = { |
| 389 | + "type": "function_call_output", |
| 390 | + "call_id": "somecall", |
| 391 | + "output": [ |
| 392 | + { |
| 393 | + "type": "input_image", |
| 394 | + "image_url": "https://example.com/image.png", |
| 395 | + } |
| 396 | + ], |
| 397 | + } |
| 398 | + |
| 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"): |
| 433 | + Converter.items_to_messages([func_output_item], strict_feature_validation=True) |
| 434 | + |
| 435 | + |
| 436 | +def test_items_to_messages_with_mixed_function_output_keeps_text_by_default( |
| 437 | + caplog: pytest.LogCaptureFixture, |
| 438 | +): |
| 439 | + """Default conversion should preserve text parts and omit unsupported non-text parts.""" |
| 440 | + func_output_item: FunctionCallOutput = { |
| 441 | + "type": "function_call_output", |
| 442 | + "call_id": "somecall", |
| 443 | + "output": [ |
| 444 | + {"type": "input_text", "text": "visible text"}, |
| 445 | + { |
| 446 | + "type": "input_image", |
| 447 | + "image_url": "https://example.com/image.png", |
| 448 | + }, |
| 449 | + ], |
| 450 | + } |
| 451 | + |
| 452 | + with caplog.at_level(logging.WARNING, logger="openai.agents"): |
| 453 | + messages = Converter.items_to_messages([func_output_item]) |
| 454 | + |
| 455 | + assert len(messages) == 1 |
| 456 | + tool_msg = messages[0] |
| 457 | + assert tool_msg["role"] == "tool" |
| 458 | + assert tool_msg["tool_call_id"] == func_output_item["call_id"] |
| 459 | + assert tool_msg["content"] == [{"type": "text", "text": "visible text"}] |
| 460 | + assert "tool output omitted" not in caplog.text |
| 461 | + |
| 462 | + |
| 463 | +def test_items_to_messages_can_preserve_non_text_function_output() -> None: |
| 464 | + """Compatible providers can opt in to preserving non-text tool output.""" |
| 465 | + func_output_item: FunctionCallOutput = { |
| 466 | + "type": "function_call_output", |
| 467 | + "call_id": "somecall", |
| 468 | + "output": [ |
| 469 | + { |
| 470 | + "type": "input_image", |
| 471 | + "image_url": "https://example.com/image.png", |
| 472 | + } |
| 473 | + ], |
| 474 | + } |
| 475 | + |
| 476 | + messages = Converter.items_to_messages( |
| 477 | + [func_output_item], |
| 478 | + preserve_tool_output_all_content=True, |
| 479 | + ) |
| 480 | + |
| 481 | + assert len(messages) == 1 |
| 482 | + tool_msg = messages[0] |
| 483 | + assert tool_msg["role"] == "tool" |
| 484 | + assert tool_msg["tool_call_id"] == func_output_item["call_id"] |
| 485 | + assert tool_msg["content"] == [ |
| 486 | + { |
| 487 | + "type": "image_url", |
| 488 | + "image_url": {"url": "https://example.com/image.png", "detail": "auto"}, |
| 489 | + } |
| 490 | + ] |
| 491 | + |
| 492 | + |
359 | 493 | def test_extract_all_and_text_content_for_strings_and_lists(): |
360 | 494 | """ |
361 | 495 | The converter provides helpers for extracting user-supplied message content |
|
0 commit comments