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(responses): bound the background stream cancel e2e so an upstream stall skips fast
mateo-berri36143b53f900
diff --git a/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py b/tests/openai_endpoints_tests/test_e2e_openai_responses_api.pyindex abae26e02cd..604f1c84d20 100644--- a/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py+++ b/tests/openai_endpoints_tests/test_e2e_openai_responses_api.py@@ -1,6 +1,10 @@+import time+import httpx-from openai import OpenAI, BadRequestError, NotFoundError, APIStatusErrorimport pytest+from openai import APIStatusError, BadRequestError, NotFoundError, OpenAI++BACKGROUND_STREAM_ADMISSION_DEADLINE_SECONDS = 90def generate_key():@@ -154,42 +158,46 @@ def test_cancel_response():def test_cancel_streaming_response():- try:- client = get_test_client()- from litellm.types.llms.openai import ResponsesAPIResponse-- stream = client.responses.create(- model="gpt-5.5",- input="just respond with the word 'ping'",- stream=True,- background=True,- )+ client = get_test_client()+ started = time.monotonic()+ stream = client.responses.create(+ model="gpt-5.5",+ input="just respond with the word 'ping'",+ stream=True,+ background=True,+ timeout=BACKGROUND_STREAM_ADMISSION_DEADLINE_SECONDS,+ )- collected_chunks = []- response_id = None+ keepalive_events = 0+ response_id = None+ with stream:for chunk in stream:print("stream chunk=", chunk)- collected_chunks.append(chunk)- # Extract response ID from the first chunk that has it- if (- response_id is None- and hasattr(chunk, "response")- and hasattr(chunk.response, "id")- ):+ if chunk.type == "keepalive":+ keepalive_events += 1+ elif getattr(chunk, "response", None) is not None:response_id = chunk.response.id+ break+ if time.monotonic() - started > BACKGROUND_STREAM_ADMISSION_DEADLINE_SECONDS:+ break++ elapsed = time.monotonic() - started+ if response_id is None and keepalive_events:+ pytest.skip(+ f"OpenAI held the background stream in keepalive for {elapsed:.0f}s "+ f"({keepalive_events} keepalive events) without creating the response"+ )+ assert response_id is not None, f"no response event within {elapsed:.0f}s of streaming a background response"- assert len(collected_chunks) > 0-- # cancel the response if we got a response ID- if response_id:- cancel_response = client.responses.cancel(response_id)- print("CANCEL streaming response=", cancel_response)- assert hasattr(cancel_response, "id")- except Exception as e:- if "Cannot cancel a completed response" in str(e):- pass- else:- raise e+ try:+ cancel_response = client.responses.cancel(response_id)+ except BadRequestError as e:+ if "Cannot cancel a completed response" not in str(e):+ raise+ print("response completed before cancel=", e)+ return+ print("CANCEL streaming response=", cancel_response)+ assert cancel_response.status == "cancelled"def test_cancel_invalid_response_id():