mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-05 01:59:27 +00:00
* fix(responses): keep the provider code and message on top-level stream error events Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(responses): log a mid-stream failure before the router falls back CustomStreamWrapper now waits for failure logging before it raises the error the router falls back on, so the cooldown lands on the deployment that failed instead of the fallback that replaced it. Also keeps numeric error codes and the top-level message on the WebSocket and ChatGPT paths. * fix(streaming): run sync failure callbacks before a failed stream falls back to another deployment The router cools a deployment down from a sync failure callback that reads the request's model_info. A failed stream submitted that callback to the logging executor without waiting, so it often ran after the router's fallback had overwritten model_info and cooled down the healthy fallback deployment instead, leaving the model group with no deployments * test(responses): cover top-level OpenAI stream error events end to end * test(streaming): read live worker pids so the second SIGKILL chaos variant sees two workers * fix(streaming): run waited sync failure logging off the shared logging pool * revert(streaming): keep failed-stream failure logging in the background Waiting for sync failure callbacks before a failed stream raises fixed the router cooldown race, but it slowed healthy calls under many failing streams with a slow callback, since the wait shares a thread pool with every call's start. The race is pre-existing on main and is tracked separately, so this PR goes back to main's logging timing and keeps only the top-level error event changes --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
255 lines
10 KiB
Python
255 lines
10 KiB
Python
import asyncio
|
|
import hashlib
|
|
import itertools
|
|
import json
|
|
import ssl
|
|
import threading
|
|
import uuid
|
|
from collections.abc import Callable, Iterator
|
|
from contextlib import contextmanager
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from queue import SimpleQueue
|
|
from typing import Final, TypeAlias
|
|
|
|
import pytest
|
|
import websockets
|
|
from integration._support.client import Gateway, eventually, gateway_from_environment, object_value, string_value
|
|
from integration._support.database import read_rows
|
|
from integration._support.process import graceful_stop_seconds, owned_proxy
|
|
from integration._support.responses_stream import RATE_LIMIT_MESSAGE, openai_error_event
|
|
from integration._support.tls import server_context, write_self_signed_cert
|
|
from pydantic import JsonValue
|
|
from websockets.asyncio.server import ServerConnection, serve
|
|
|
|
pytestmark: Final = pytest.mark.timeout(2 * graceful_stop_seconds() + 120)
|
|
|
|
PROVIDER_MODEL: Final = "ws-peer-model"
|
|
USAGE: Final = {"input_tokens": 5, "output_tokens": 2, "total_tokens": 7}
|
|
TERMINAL: Final = frozenset({"response.completed", "response.failed", "error"})
|
|
|
|
_Script: TypeAlias = Callable[[str], tuple[dict[str, JsonValue], ...]]
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Peer:
|
|
url: str
|
|
paths: SimpleQueue[str]
|
|
frames: SimpleQueue[dict[str, JsonValue]]
|
|
|
|
|
|
def _events(response_id: str, text: str) -> tuple[dict[str, JsonValue], ...]:
|
|
message: Final = {
|
|
"type": "message",
|
|
"id": f"msg_{response_id}",
|
|
"status": "completed",
|
|
"role": "assistant",
|
|
"content": [{"type": "output_text", "text": text, "annotations": []}],
|
|
}
|
|
response: Final = {"id": response_id, "object": "response", "created_at": 1700000000, "model": PROVIDER_MODEL}
|
|
return (
|
|
{"type": "response.created", "response": {**response, "status": "in_progress", "output": []}},
|
|
{
|
|
"type": "response.output_text.delta",
|
|
"item_id": f"msg_{response_id}",
|
|
"output_index": 0,
|
|
"content_index": 0,
|
|
"delta": text,
|
|
},
|
|
{
|
|
"type": "response.completed",
|
|
"response": {**response, "status": "completed", "output": [message], "usage": USAGE},
|
|
},
|
|
)
|
|
|
|
|
|
def _seven(response_id: str) -> tuple[dict[str, JsonValue], ...]:
|
|
return _events(response_id, "seven")
|
|
|
|
|
|
def _top_level_rate_limit(response_id: str) -> tuple[dict[str, JsonValue], ...]:
|
|
created: Final = _events(response_id, "")[0]
|
|
return (created, dict(openai_error_event("rate_limit_exceeded", RATE_LIMIT_MESSAGE)))
|
|
|
|
|
|
async def _answer(
|
|
connection: ServerConnection,
|
|
paths: SimpleQueue[str],
|
|
frames: SimpleQueue[dict[str, JsonValue]],
|
|
script: _Script,
|
|
) -> None:
|
|
paths.put(connection.request.path if connection.request is not None else "")
|
|
turns: Final = itertools.count(1)
|
|
async for raw in connection:
|
|
frame: Final = json.loads(raw)
|
|
frames.put(frame)
|
|
if frame.get("type") != "response.create":
|
|
continue
|
|
for event in script(f"resp_peer_{next(turns)}"):
|
|
await connection.send(json.dumps(event))
|
|
|
|
|
|
async def _serve(
|
|
tls: ssl.SSLContext,
|
|
paths: SimpleQueue[str],
|
|
frames: SimpleQueue[dict[str, JsonValue]],
|
|
ports: SimpleQueue[int],
|
|
stop: asyncio.Event,
|
|
script: _Script,
|
|
) -> None:
|
|
async with serve(lambda connection: _answer(connection, paths, frames, script), "127.0.0.1", 0, ssl=tls) as server:
|
|
ports.put(next(iter(server.sockets)).getsockname()[1])
|
|
await stop.wait()
|
|
|
|
|
|
@contextmanager
|
|
def responses_peer(cert: tuple[Path, Path], script: _Script = _seven) -> Iterator[Peer]:
|
|
loop: Final = asyncio.new_event_loop()
|
|
stop: Final = asyncio.Event()
|
|
paths: Final = SimpleQueue[str]()
|
|
frames: Final = SimpleQueue[dict[str, JsonValue]]()
|
|
ports: Final = SimpleQueue[int]()
|
|
thread: Final = threading.Thread(
|
|
target=loop.run_until_complete,
|
|
args=(_serve(server_context(*cert), paths, frames, ports, stop, script),),
|
|
daemon=True,
|
|
)
|
|
thread.start()
|
|
try:
|
|
yield Peer(f"https://127.0.0.1:{ports.get(timeout=10)}/v1", paths, frames)
|
|
finally:
|
|
loop.call_soon_threadsafe(stop.set)
|
|
thread.join(timeout=10)
|
|
loop.close()
|
|
|
|
|
|
def _create(model: str, text: str, previous_response_id: str | None = None) -> str:
|
|
return json.dumps(
|
|
{
|
|
"type": "response.create",
|
|
"model": model,
|
|
"store": True,
|
|
"input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": text}]}],
|
|
**({} if previous_response_id is None else {"previous_response_id": previous_response_id}),
|
|
}
|
|
)
|
|
|
|
|
|
async def _turn(connection: websockets.ClientConnection, frame: str) -> tuple[dict[str, JsonValue], ...]:
|
|
await connection.send(frame)
|
|
return await _until_terminal(connection, ())
|
|
|
|
|
|
async def _until_terminal(
|
|
connection: websockets.ClientConnection, received: tuple[dict[str, JsonValue], ...]
|
|
) -> tuple[dict[str, JsonValue], ...]:
|
|
event: Final = json.loads(await asyncio.wait_for(connection.recv(), timeout=20))
|
|
collected: Final = (*received, event)
|
|
if event.get("type") in TERMINAL or len(collected) >= 50:
|
|
return collected
|
|
return await _until_terminal(connection, collected)
|
|
|
|
|
|
async def _session(
|
|
proxy_url: str, key: str, model: str, texts: tuple[str, ...]
|
|
) -> tuple[tuple[dict[str, JsonValue], ...], ...]:
|
|
proxy: Final = proxy_url.rstrip("/").replace("http://", "ws://")
|
|
async with websockets.connect(
|
|
f"{proxy}/v1/responses?model={model}",
|
|
additional_headers={"Authorization": f"Bearer {key}"},
|
|
open_timeout=10,
|
|
) as connection:
|
|
first: Final = await _turn(connection, _create(model, texts[0]))
|
|
if len(texts) == 1:
|
|
return (first,)
|
|
previous: Final = str(first[-1]["response"]["id"])
|
|
second: Final = await _turn(connection, _create(model, texts[1], previous))
|
|
return (first, second)
|
|
|
|
|
|
def _completed(events: tuple[dict[str, JsonValue], ...]) -> dict[str, JsonValue]:
|
|
assert events[-1]["type"] == "response.completed", [event.get("type") for event in events]
|
|
return events[-1]["response"]
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def cert(tmp_path_factory: pytest.TempPathFactory) -> tuple[Path, Path]:
|
|
return write_self_signed_cert(tmp_path_factory.mktemp("responses-ws-cert"))
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def candidate(tmp_path_factory: pytest.TempPathFactory, cert: tuple[Path, Path]) -> Iterator[Gateway]:
|
|
with gateway_from_environment() as base:
|
|
with owned_proxy(base, tmp_path_factory.mktemp("responses-ws"), {"SSL_CERT_FILE": str(cert[0])}) as proxy:
|
|
yield proxy
|
|
|
|
|
|
def _drain(queue: SimpleQueue[dict[str, JsonValue]]) -> tuple[dict[str, JsonValue], ...]:
|
|
return tuple(queue.get_nowait() for _ in range(queue.qsize()))
|
|
|
|
|
|
def test_a_response_create_frame_streams_from_the_provider_socket_back_to_the_client(
|
|
candidate: Gateway, cert: tuple[Path, Path]
|
|
) -> None:
|
|
with responses_peer(cert) as peer, candidate.scenario() as scenario:
|
|
model: Final = scenario.model(model=f"openai/{PROVIDER_MODEL}", api_base=peer.url)
|
|
key: Final = scenario.key(models=[model])
|
|
text: Final = f"say seven {uuid.uuid4().hex}"
|
|
(events,) = asyncio.run(_session(str(candidate.client.base_url), key, model, (text,)))
|
|
assert [event["type"] for event in events] == [
|
|
"response.created",
|
|
"response.output_text.delta",
|
|
"response.completed",
|
|
]
|
|
completed: Final = _completed(events)
|
|
assert completed["status"] == "completed"
|
|
assert completed["usage"] == USAGE
|
|
assert peer.paths.get_nowait() == f"/v1/responses?model={PROVIDER_MODEL}"
|
|
(forwarded,) = _drain(peer.frames)
|
|
assert forwarded["type"] == "response.create"
|
|
assert forwarded["model"] == PROVIDER_MODEL
|
|
assert forwarded["input"][0]["content"][0]["text"] == text
|
|
|
|
|
|
def test_previous_response_id_from_the_first_turn_reaches_the_provider_as_its_own_id(
|
|
candidate: Gateway, cert: tuple[Path, Path]
|
|
) -> None:
|
|
with responses_peer(cert) as peer, candidate.scenario() as scenario:
|
|
model: Final = scenario.model(model=f"openai/{PROVIDER_MODEL}", api_base=peer.url)
|
|
key: Final = scenario.key(models=[model])
|
|
texts: Final = (f"remember seven {uuid.uuid4().hex}", f"which number {uuid.uuid4().hex}")
|
|
first, second = asyncio.run(_session(str(candidate.client.base_url), key, model, texts))
|
|
assert _completed(first)["status"] == "completed"
|
|
assert str(_completed(first)["id"]).startswith("resp_") and _completed(first)["id"] != "resp_peer_1"
|
|
assert _completed(second)["status"] == "completed"
|
|
assert peer.paths.qsize() == 1
|
|
forwarded: Final = _drain(peer.frames)
|
|
assert [frame["input"][0]["content"][0]["text"] for frame in forwarded] == list(texts)
|
|
assert "previous_response_id" not in forwarded[0]
|
|
assert forwarded[1]["previous_response_id"] == "resp_peer_1"
|
|
|
|
|
|
def test_a_top_level_error_frame_reaches_the_client_and_logs_the_provider_code_and_message(
|
|
candidate: Gateway, cert: tuple[Path, Path]
|
|
) -> None:
|
|
with responses_peer(cert, _top_level_rate_limit) as peer, candidate.scenario() as scenario:
|
|
model: Final = scenario.model(model=f"openai/{PROVIDER_MODEL}", api_base=peer.url)
|
|
key: Final = scenario.key(models=[model])
|
|
text: Final = f"say seven {uuid.uuid4().hex}"
|
|
(events,) = asyncio.run(_session(str(candidate.client.base_url), key, model, (text,)))
|
|
assert [event["type"] for event in events] == ["response.created", "error"], events
|
|
assert (events[-1]["code"], events[-1]["message"]) == ("rate_limit_exceeded", RATE_LIMIT_MESSAGE), events
|
|
(forwarded,) = _drain(peer.frames)
|
|
assert forwarded["input"][0]["content"][0]["text"] == text
|
|
(row,) = eventually(
|
|
lambda: read_rows(
|
|
'SELECT status, metadata FROM "LiteLLM_SpendLogs" WHERE api_key = %s',
|
|
(hashlib.sha256(key.encode()).hexdigest(),),
|
|
),
|
|
lambda rows: len(rows) >= 1,
|
|
seconds=70,
|
|
)
|
|
assert row["status"] == "failure", row
|
|
error: Final = object_value(object_value(row["metadata"])["error_information"])
|
|
assert str(error["error_code"]) == "429", error
|
|
assert RATE_LIMIT_MESSAGE in string_value(error["error_message"]), error
|