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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
test(e2e): cover /v1/responses openai basic nonstream and stream (#33830)
devin-ai-integration[bot]a1fb07f42cd3
diff --git a/tests/e2e/e2e_http.py b/tests/e2e/e2e_http.pyindex 009529e09db..692f951d08e 100644--- a/tests/e2e/e2e_http.py+++ b/tests/e2e/e2e_http.py@@ -130,6 +130,7 @@ class StreamingResponse(BaseModel):headers: dict[str, str] = {}body: strchunks: int = 0 # streamed events (0 for non-streaming)+ stream_events: list[str] = []# First in-stream error event, if any. A streamed call commits its HTTP 200# before the upstream completes, so upstream failures (e.g. insufficient# quota) arrive as SSE error events inside an otherwise-successful response;@@ -305,10 +306,16 @@ def _streaming_outcome(resp: requests.Response, stream: bool) -> StreamingResponlines = cast("Iterator[bytes]", resp.iter_lines())chunks = 0stream_error: str | None = None+ stream_events: list[str] = []for line in lines:if not line:continuechunks += 1+ decoded_line = line.decode(errors="replace")+ if decoded_line.startswith("data: "):+ payload = decoded_line.removeprefix("data: ")+ if payload != "[DONE]":+ stream_events.append(payload)if stream_error is None and (line.startswith(b"event: error")or b'"type":"error"' in line@@ -324,6 +331,7 @@ def _streaming_outcome(resp: requests.Response, stream: bool) -> StreamingResponheaders=headers,body="<streamed>",chunks=chunks,+ stream_events=stream_events,stream_error=stream_error,)diff --git a/tests/e2e/llm_translation/endpoints_client.py b/tests/e2e/llm_translation/endpoints_client.pyindex e339922b4d1..54461405a23 100644--- a/tests/e2e/llm_translation/endpoints_client.py+++ b/tests/e2e/llm_translation/endpoints_client.py@@ -10,6 +10,7 @@ so the assertion is on real content, not just a 200.from __future__ import annotationsfrom dataclasses import dataclass+from typing import Literalfrom pydantic import BaseModel@@ -22,6 +23,7 @@ class ResponsesRequest(BaseModel):model: strinput: strinstructions: str | None = None+ stream: bool = Falseclass MessagesRequest(BaseModel):@@ -100,6 +102,19 @@ class ResponsesResult(BaseModel):)+class ResponsesStreamEvent(BaseModel):+ event_id: str | None = None+++class ResponsesStreamEventType(BaseModel):+ type: str+++class ResponsesOutputTextDeltaEvent(ResponsesStreamEvent):+ type: Literal["response.output_text.delta"]+ delta: str++class AnthropicContentBlock(BaseModel):type: str | None = Nonetext: str | None = None@@ -164,18 +179,29 @@ class EndpointsClient:def delete_model(self, model_id: str) -> None:self.proxy.delete_model(model_id)- def _send(self, path: str, key: str, body: BaseModel) -> StreamingResponse:+ def _send(+ self, path: str, key: str, body: BaseModel, *, stream: bool = False+ ) -> StreamingResponse:return self.proxy.transport.send(- path, headers=self.proxy.transport.bearer(key), json=body+ path,+ headers=self.proxy.transport.bearer(key),+ json=body,+ stream=stream,)- def responses(self, key: str, model: str, text: str) -> StreamingResponse:+ def responses(+ self, key: str, model: str, text: str, *, stream: bool = False+ ) -> StreamingResponse:return self._send("/v1/responses",key,ResponsesRequest(- model=model, input=text, instructions="You are a helpful assistant"+ model=model,+ input=text,+ instructions="You are a helpful assistant",+ stream=stream,),+ stream=stream,)def messages(diff --git a/tests/e2e/llm_translation/test_responses_e2e.py b/tests/e2e/llm_translation/test_responses_e2e.pyindex 743de79880f..91c0759f233 100644--- a/tests/e2e/llm_translation/test_responses_e2e.py+++ b/tests/e2e/llm_translation/test_responses_e2e.py@@ -8,10 +8,16 @@ litellm-regression-tests/tests/test_inference_endpoints.py.from __future__ import annotationsimport pytest+from pydantic import ValidationErrorfrom e2e_config import unique_markerfrom e2e_http import require_successful_call-from endpoints_client import EndpointsClient, ResponsesResult+from endpoints_client import (+ EndpointsClient,+ ResponsesOutputTextDeltaEvent,+ ResponsesResult,+ ResponsesStreamEventType,+)from lifecycle import ResourceManagerfrom models import LiteLLMParamsBody@@ -19,6 +25,7 @@ pytestmark = pytest.mark.e2eclass TestResponses:+ @pytest.mark.covers("llm.responses.openai.basic.nonstream.works")def test_responses_returns_completion(self, endpoints_client: EndpointsClient, resources: ResourceManager) -> None:@@ -34,3 +41,39 @@ class TestResponses:require_successful_call(result)parsed = ResponsesResult.model_validate_json(result.body)assert parsed.text.strip(), f"/responses returned no output text: {result.body[:300]}"++ @pytest.mark.covers("llm.responses.openai.basic.stream.works")+ def test_responses_streaming_returns_completion(+ self, endpoints_client: EndpointsClient, resources: ResourceManager+ ) -> None:+ model = f"e2e-responses-{unique_marker()}"+ model_id = endpoints_client.create_model(+ model,+ LiteLLMParamsBody(model="openai/gpt-4o-mini", api_key="os.environ/OPENAI_API_KEY"),+ )+ resources.defer(lambda: endpoints_client.delete_model(model_id))+ key = resources.key()++ result = endpoints_client.responses(key, model, "reply with one word", stream=True)+ require_successful_call(result)+ delta_events = tuple(+ parsed+ for event in result.stream_events+ if (parsed := _parse_stream_event(event)) is not None+ )++ assert any(event.delta for event in delta_events), "responses stream returned no text deltas"+ assert result.stream_events, "responses stream returned no events"+ assert (+ ResponsesStreamEventType.model_validate_json(result.stream_events[-1]).type+ == "response.completed"+ ), "responses stream did not terminate with response.completed"+++def _parse_stream_event(+ event: str,+) -> ResponsesOutputTextDeltaEvent | None:+ try:+ return ResponsesOutputTextDeltaEvent.model_validate_json(event)+ except ValidationError:+ return None