Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

test(e2e): cover Anthropic /chat/completions streaming and tool calls

Yuneng Jiang98784360e851
diff --git a/tests/e2e/llm_translation/test_chat_completions_regression_e2e.py b/tests/e2e/llm_translation/test_chat_completions_regression_e2e.py
index 68c0dfab897..87bd32d8dab 100644
--- a/tests/e2e/llm_translation/test_chat_completions_regression_e2e.py
+++ b/tests/e2e/llm_translation/test_chat_completions_regression_e2e.py
@@ -11,7 +11,7 @@ fails that provider's row here.
The per-provider classes below cover the OpenAI-compatible /chat/completions
translation for providers customers reach by registering their own deployment
-via /model/new (Cohere, Gemini, hosted_vllm), each deleted on teardown.
+via /model/new (Cohere, Gemini, hosted_vllm, Anthropic), each deleted on teardown.
"""
from __future__ import annotations
@@ -46,6 +46,7 @@ pytestmark = pytest.mark.e2e
COHERE_BACKEND = "cohere/command-r-08-2024"
GEMINI_BACKEND = "gemini/gemini-2.5-flash"
OPENAI_BACKEND = "openai/gpt-5.6"
+ANTHROPIC_BACKEND = "anthropic/claude-haiku-4-5-20251001"
BEDROCK_CONVERSE_BACKEND = "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"
@@ -746,3 +747,98 @@ class TestBedrockConverseChatCompletions:
response = unwrap(client.proxy.chat(key, ChatBody(model=model, messages=_vision_messages(), max_tokens=32)))
_assert_describes_cat(response)
+
+
+class TestAnthropicChatCompletions:
+ """Anthropic via the OpenAI-compatible /chat/completions path, the translation
+ customers on the OpenAI SDK rely on when they route to Claude. The streamed call
+ must deliver real content deltas, and a tool-forced call must come back as a
+ well-formed tool_call on both the non-streamed and streamed paths.
+ """
+
+ def _register(self, client: PassthroughClient, resources: ResourceManager, prefix: str) -> str:
+ model = f"{prefix}-{unique_marker()}"
+ model_id = client.proxy.create_model(
+ model, LiteLLMParamsBody(model=ANTHROPIC_BACKEND, api_key="os.environ/ANTHROPIC_API_KEY")
+ )
+ resources.defer(lambda: client.proxy.delete_model(model_id))
+ return model
+
+ @pytest.mark.covers(
+ "llm.chat_completions.anthropic.basic.stream.works",
+ exercised_on=["chat_completions"],
+ )
+ def test_anthropic_chat_streams_real_content(
+ self, client: PassthroughClient, resources: ResourceManager
+ ) -> None:
+ model = self._register(client, resources, "e2e-anthropic-stream")
+ key = resources.key()
+
+ result = client.proxy.chat_stream(
+ key,
+ ChatBody(
+ model=model,
+ messages=[
+ ChatMessage(role="user", content=f"Count from 1 to 5, one number per line. {unique_marker()}")
+ ],
+ max_tokens=64,
+ stream=True,
+ ),
+ )
+ _assert_streamed_completion(result)
+
+ @pytest.mark.covers(
+ "llm.chat_completions.anthropic.tool_use.nonstream.works",
+ exercised_on=["chat_completions"],
+ )
+ def test_anthropic_chat_returns_tool_call(
+ self, client: PassthroughClient, resources: ResourceManager
+ ) -> None:
+ model = self._register(client, resources, "e2e-anthropic-tool")
+ key = resources.key()
+
+ response = unwrap(
+ client.proxy.chat(
+ key,
+ ChatBody(
+ model=model,
+ messages=[
+ ChatMessage(role="user", content="What is the weather in San Francisco? Use the get_weather tool.")
+ ],
+ tools=[_WEATHER_TOOL],
+ tool_choice="required",
+ max_tokens=128,
+ ),
+ )
+ )
+ _assert_weather_tool_call(response)
+
+ @pytest.mark.covers(
+ "llm.chat_completions.anthropic.tool_use.stream.works",
+ exercised_on=["chat_completions"],
+ )
+ def test_anthropic_chat_streams_tool_call(
+ self, client: PassthroughClient, resources: ResourceManager
+ ) -> None:
+ model = self._register(client, resources, "e2e-anthropic-tool-stream")
+ key = resources.key()
+
+ result = client.proxy.chat_stream(
+ key,
+ ChatBody(
+ model=model,
+ messages=[
+ ChatMessage(role="user", content="What is the weather in San Francisco? Use the get_weather tool.")
+ ],
+ tools=[_WEATHER_TOOL],
+ tool_choice="required",
+ max_tokens=128,
+ stream=True,
+ ),
+ )
+ assert result.ok and result.is_streaming, f"tool stream was not established: {result}"
+ assert result.stream_error is None, f"tool stream carried an error event: {result.stream_error}"
+ name, arguments = _streamed_tool_call(result.stream_events)
+ assert name == "get_weather", f"streamed tool call named {name!r}: {result.stream_events[:5]}"
+ args = _WeatherArgs.model_validate_json(arguments)
+ assert args.location.strip(), f"streamed tool call arguments missing location: {arguments!r}"