Merge pull request #174 from AysunItai/fix/anthropic-provider-response-format
fix: align AnthropicProvider.complete with LLMProvider (response_format)
This commit is contained in:
@@ -67,6 +67,7 @@ class AnthropicProvider(LLMProvider):
|
||||
system: str = "",
|
||||
tools: list[Tool] | None = None,
|
||||
max_tokens: int = 1024,
|
||||
response_format: dict[str, Any] | None = None,
|
||||
json_mode: bool = False,
|
||||
) -> LLMResponse:
|
||||
"""Generate a completion from Claude (via LiteLLM)."""
|
||||
@@ -75,6 +76,7 @@ class AnthropicProvider(LLMProvider):
|
||||
system=system,
|
||||
tools=tools,
|
||||
max_tokens=max_tokens,
|
||||
response_format=response_format,
|
||||
json_mode=json_mode,
|
||||
)
|
||||
|
||||
|
||||
@@ -330,6 +330,31 @@ class TestAnthropicProviderBackwardCompatibility:
|
||||
assert result.content == "The time is 3:00 PM."
|
||||
mock_completion.assert_called_once()
|
||||
|
||||
@patch("litellm.completion")
|
||||
def test_anthropic_provider_passes_response_format(self, mock_completion):
|
||||
"""Test that AnthropicProvider accepts and forwards response_format."""
|
||||
# Setup mock
|
||||
mock_response = MagicMock()
|
||||
mock_response.choices = [MagicMock()]
|
||||
mock_response.choices[0].message.content = "{}"
|
||||
mock_response.choices[0].finish_reason = "stop"
|
||||
mock_response.model = "claude-3-haiku-20240307"
|
||||
mock_response.usage.prompt_tokens = 10
|
||||
mock_response.usage.completion_tokens = 5
|
||||
mock_completion.return_value = mock_response
|
||||
|
||||
provider = AnthropicProvider(api_key="test-key")
|
||||
fmt = {"type": "json_object"}
|
||||
|
||||
provider.complete(
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
response_format=fmt
|
||||
)
|
||||
|
||||
# Verify it was passed to litellm
|
||||
call_kwargs = mock_completion.call_args[1]
|
||||
assert call_kwargs["response_format"] == fmt
|
||||
|
||||
|
||||
class TestJsonMode:
|
||||
"""Test json_mode parameter for structured JSON output via prompt engineering."""
|
||||
|
||||
Reference in New Issue
Block a user