|
| 1 | +import pytest |
| 2 | +from pydantic import TypeAdapter, ValidationError |
| 3 | + |
| 4 | +from llama_stack.apis.agents.openai_responses import OpenAIResponsesToolChoice |
| 5 | +from llama_stack.apis.tools.openai_tool_choice import ( |
| 6 | + ToolChoiceAllowed, |
| 7 | + ToolChoiceCustom, |
| 8 | + ToolChoiceFunction, |
| 9 | + ToolChoiceMcp, |
| 10 | + ToolChoiceOptions, |
| 11 | + ToolChoiceTypes, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def test_tool_choice_discriminated_options(): |
| 16 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 17 | + |
| 18 | + cases = [ |
| 19 | + ({"type": "function", "name": "search"}, ToolChoiceFunction, "function"), |
| 20 | + ({"type": "mcp", "server_label": "deepwiki"}, ToolChoiceMcp, "mcp"), |
| 21 | + ({"type": "custom", "name": "my_tool"}, ToolChoiceCustom, "custom"), |
| 22 | + ( |
| 23 | + { |
| 24 | + "type": "allowed_tools", |
| 25 | + "mode": "auto", |
| 26 | + "tools": [{"type": "function", "name": "foo"}], |
| 27 | + }, |
| 28 | + ToolChoiceAllowed, |
| 29 | + "allowed_tools", |
| 30 | + ), |
| 31 | + ] |
| 32 | + |
| 33 | + for payload, expected_cls, expected_type in cases: |
| 34 | + obj = adapter.validate_python(payload) |
| 35 | + assert isinstance(obj, expected_cls) |
| 36 | + assert getattr(obj, "type") == expected_type |
| 37 | + |
| 38 | + dumped = obj.model_dump() |
| 39 | + reparsed = adapter.validate_python(dumped) |
| 40 | + assert isinstance(reparsed, expected_cls) |
| 41 | + assert reparsed.model_dump() == dumped |
| 42 | + |
| 43 | + |
| 44 | +def test_tool_choice_literal_options(): |
| 45 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 46 | + options_adapter = TypeAdapter(ToolChoiceOptions) |
| 47 | + |
| 48 | + for v in ("none", "auto", "required"): |
| 49 | + # Validate via the specific literal adapter |
| 50 | + assert options_adapter.validate_python(v) == v |
| 51 | + # And via the top-level union adapter |
| 52 | + assert adapter.validate_python(v) == v |
| 53 | + |
| 54 | + |
| 55 | +def test_tool_choice_rejects_invalid_value(): |
| 56 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 57 | + |
| 58 | + with pytest.raises(ValidationError): |
| 59 | + adapter.validate_python("invalid") |
| 60 | + with pytest.raises(ValidationError): |
| 61 | + adapter.validate_python({"type": "unknown_variant"}) |
| 62 | + |
| 63 | + |
| 64 | +def test_tool_choice_types_accepts_each_variant_value(): |
| 65 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 66 | + |
| 67 | + allowed_values = [ |
| 68 | + "file_search", |
| 69 | + "web_search_preview", |
| 70 | + "computer_use_preview", |
| 71 | + "web_search_preview_2025_03_11", |
| 72 | + "image_generation", |
| 73 | + "code_interpreter", |
| 74 | + ] |
| 75 | + |
| 76 | + for v in allowed_values: |
| 77 | + obj = adapter.validate_python({"type": v}) |
| 78 | + assert isinstance(obj, ToolChoiceTypes) |
| 79 | + assert obj.type == v |
| 80 | + assert obj.model_dump() == {"type": v} |
| 81 | + |
| 82 | + |
| 83 | +def test_tool_choice_rejects_invalid_discriminator_value(): |
| 84 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 85 | + with pytest.raises(ValidationError): |
| 86 | + adapter.validate_python({"type": "unknown_variant"}) |
| 87 | + |
| 88 | + |
| 89 | +def test_tool_choice_rejects_missing_required_fields(): |
| 90 | + adapter = TypeAdapter(OpenAIResponsesToolChoice) |
| 91 | + # Missing "name" for function |
| 92 | + with pytest.raises(ValidationError): |
| 93 | + adapter.validate_python({"type": "function"}) |
0 commit comments