|
| 1 | +# Copyright (c) The OGX Contributors. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in |
| 5 | +# the root directory of this source tree. |
| 6 | + |
| 7 | +from unittest.mock import AsyncMock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from ogx.providers.remote.inference.anthropic.anthropic import AnthropicInferenceAdapter |
| 12 | +from ogx.providers.remote.inference.anthropic.config import AnthropicConfig |
| 13 | +from ogx_api.inference.models import OpenAIChatCompletionRequestWithExtraBody |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def adapter(): |
| 18 | + config = AnthropicConfig(api_key="test-key") |
| 19 | + return AnthropicInferenceAdapter(config=config) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "input_params,expected_params", |
| 24 | + [ |
| 25 | + ({}, {"type": "object"}), |
| 26 | + ({"type": "object", "properties": {}}, {"type": "object", "properties": {}}), |
| 27 | + ], |
| 28 | + ids=["empty", "already-valid"], |
| 29 | +) |
| 30 | +async def test_empty_tool_parameters_normalized(adapter, input_params, expected_params): |
| 31 | + """Anthropic rejects parameters: {} but OpenAI accepts it; the adapter normalizes.""" |
| 32 | + params = OpenAIChatCompletionRequestWithExtraBody( |
| 33 | + model="claude-sonnet-4-6", |
| 34 | + messages=[{"role": "user", "content": "hi"}], |
| 35 | + tools=[{"type": "function", "function": {"name": "my_func", "parameters": input_params}}], |
| 36 | + ) |
| 37 | + |
| 38 | + with patch.object(type(adapter).__mro__[1], "openai_chat_completion", new_callable=AsyncMock) as mock_super: |
| 39 | + mock_super.return_value = {} |
| 40 | + await adapter.openai_chat_completion(params) |
| 41 | + |
| 42 | + assert params.tools[0]["function"]["parameters"] == expected_params |
0 commit comments