mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-26 22:30:11 +00:00
* test(integration): wait on the right signal in five flaky integration contracts The Prometheus shared-label tests waited for the latency metric and then read the request counter, which the logger writes after an await. They now wait for the counter The auto-router session poll returned on the first 200 while the caller expected two turns, so it now waits for the expected turn count The S3 queue cap test let its failing sink recover on a 15s clock that could expire mid-flush. The sink now stays down until a flush fails after every event is queued K3 normally takes 168-175s against a 180s limit, so the limit is now 300s The MCP env var browser spec reused an idle keep-alive socket that the proxy closes after 5s, so its API calls now send Connection: close * test(integration): add until_asserted so a wait polls the exact asserts it guards eventually() takes a predicate separate from the asserts that follow it, which lets a test wait on one value and then assert on another written later. until_asserted reruns a block of asserts until they all pass and re-raises the last failure on timeout The Prometheus shared-label tests and the auto-router session check now use it
641 lines
29 KiB
Python
641 lines
29 KiB
Python
import hashlib
|
|
import json
|
|
import uuid
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from typing import Final
|
|
|
|
import httpx
|
|
import pytest
|
|
import yaml
|
|
from integration._support.client import JSON_OBJECT, Gateway, eventually, object_value, string_value, until_asserted
|
|
from integration._support.database import read_rows, scratch_database
|
|
from integration._support.process import owned_proxy
|
|
from integration._support.wire import Reply, Request, Wire, wire_server
|
|
from pydantic import JsonValue, TypeAdapter
|
|
|
|
JSON_ARRAY: Final = TypeAdapter(list[JsonValue])
|
|
|
|
|
|
def _assistant_content(response: httpx.Response) -> str:
|
|
body: Final = JSON_OBJECT.validate_json(response.content)
|
|
choices: Final = JSON_ARRAY.validate_python(body["choices"])
|
|
assert len(choices) == 1, response.text
|
|
choice: Final = object_value(choices[0])
|
|
message: Final = object_value(choice["message"])
|
|
return string_value(message["content"])
|
|
|
|
|
|
def _tier_peer(model: str, prompts: tuple[str, ...]) -> Callable[[Request], Reply]:
|
|
def respond(request: Request) -> Reply:
|
|
if request.method == "GET":
|
|
return Reply(body=b'{"object":"list","data":[]}')
|
|
assert request.method == "POST", request.method
|
|
if request.target == "/v1/responses":
|
|
return Reply(
|
|
body=json.dumps(
|
|
{
|
|
"id": "resp-" + uuid.uuid4().hex[:8],
|
|
"object": "response",
|
|
"created_at": 1700000000,
|
|
"status": "completed",
|
|
"model": model,
|
|
"output": [
|
|
{
|
|
"id": "msg-" + uuid.uuid4().hex[:8],
|
|
"type": "message",
|
|
"status": "completed",
|
|
"role": "assistant",
|
|
"content": [
|
|
{
|
|
"type": "output_text",
|
|
"text": "synthetic tier response",
|
|
"annotations": [],
|
|
}
|
|
],
|
|
}
|
|
],
|
|
"usage": {"input_tokens": 9, "output_tokens": 3, "total_tokens": 12},
|
|
}
|
|
).encode()
|
|
)
|
|
assert request.target == "/v1/chat/completions", request.target
|
|
body: Final = JSON_OBJECT.validate_json(request.body)
|
|
expected_bodies: Final = tuple(
|
|
{"model": model, "messages": [{"role": "user", "content": prompt}]} for prompt in prompts
|
|
)
|
|
assert body in expected_bodies, request.body.decode()
|
|
return Reply(
|
|
body=json.dumps(
|
|
{
|
|
"id": "chatcmpl-" + uuid.uuid4().hex[:8],
|
|
"object": "chat.completion",
|
|
"created": 1700000000,
|
|
"model": model,
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": "synthetic tier response"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
"usage": {"prompt_tokens": 9, "completion_tokens": 3, "total_tokens": 12},
|
|
}
|
|
).encode()
|
|
)
|
|
|
|
return respond
|
|
|
|
|
|
def _deployment(
|
|
alias: str,
|
|
model: str,
|
|
wire: Wire,
|
|
input_cost_per_token: float,
|
|
output_cost_per_token: float,
|
|
) -> dict[str, JsonValue]:
|
|
return {
|
|
"model_name": alias,
|
|
"litellm_params": {
|
|
"model": f"openai/{model}",
|
|
"api_base": f"{wire.url}/v1",
|
|
"api_key": "synthetic-auto-router-key",
|
|
},
|
|
"model_info": {
|
|
"id": f"deployment-{alias}",
|
|
"input_cost_per_token": input_cost_per_token,
|
|
"output_cost_per_token": output_cost_per_token,
|
|
},
|
|
}
|
|
|
|
|
|
def _session_response(
|
|
gateway: Gateway,
|
|
key: str,
|
|
session_id: str,
|
|
database_url: str | None = None,
|
|
stored_session_id: str | None = None,
|
|
seconds: float = 70,
|
|
return_last_on_timeout: bool = False,
|
|
) -> httpx.Response:
|
|
if database_url is None or stored_session_id is None:
|
|
return eventually(
|
|
lambda: gateway.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": session_id},
|
|
key=key,
|
|
),
|
|
lambda response: response.status_code == 200,
|
|
seconds=seconds,
|
|
return_last_on_timeout=return_last_on_timeout,
|
|
)
|
|
|
|
key_hash: Final = hashlib.sha256(key.encode()).hexdigest()
|
|
|
|
def read_response_state() -> tuple[httpx.Response, bool]:
|
|
rows: Final = read_rows(
|
|
'SELECT session_id FROM "LiteLLM_AutoRouterSession" WHERE api_key = %s AND session_id = %s',
|
|
(key_hash, stored_session_id),
|
|
database_url=database_url,
|
|
)
|
|
response: Final = gateway.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": session_id},
|
|
key=key,
|
|
)
|
|
return response, bool(rows)
|
|
|
|
response_state: Final = eventually(
|
|
read_response_state,
|
|
lambda state: state[0].status_code == 200 or state[1],
|
|
seconds=seconds,
|
|
return_last_on_timeout=return_last_on_timeout,
|
|
)
|
|
return response_state[0]
|
|
|
|
|
|
def _assert_rollup_row(
|
|
api_key: str,
|
|
database_url: str,
|
|
router_name: str,
|
|
expected_session_id: str,
|
|
) -> None:
|
|
key_hash: Final = hashlib.sha256(api_key.encode()).hexdigest()
|
|
rows: Final = read_rows(
|
|
"SELECT session_id, router_name, router_type, last_model, turns, spend, saved_spend, "
|
|
"savings_estimated_turns, savings_estimated_actual_spend, savings_estimated_saved_spend, "
|
|
"savings_estimated_baseline_models, baseline_models, tier_turns "
|
|
'FROM "LiteLLM_AutoRouterSession" WHERE api_key = %s AND session_id = %s AND router_name = %s',
|
|
(key_hash, expected_session_id, router_name),
|
|
database_url=database_url,
|
|
)
|
|
assert len(rows) == 1, rows
|
|
assert rows[0] == {
|
|
"session_id": expected_session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"last_model": "openai/integration-simple",
|
|
"turns": 1,
|
|
"spend": pytest.approx(0.000015),
|
|
"saved_spend": pytest.approx(0.000135),
|
|
"savings_estimated_turns": 1,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000015),
|
|
"savings_estimated_saved_spend": pytest.approx(0.000135),
|
|
"savings_estimated_baseline_models": {"openai/integration-baseline": 1},
|
|
"baseline_models": {"openai/integration-baseline": 1},
|
|
"tier_turns": {"SIMPLE": 1},
|
|
}
|
|
|
|
|
|
def _rollup_row(api_key: str, database_url: str, router_name: str, session_id: str) -> dict[str, object]:
|
|
key_hash: Final = hashlib.sha256(api_key.encode()).hexdigest()
|
|
rows: Final = read_rows(
|
|
"SELECT session_id, router_name, router_type, last_model, turns, spend, saved_spend, "
|
|
"savings_estimated_turns, savings_estimated_actual_spend, savings_estimated_saved_spend, "
|
|
"savings_estimated_baseline_models, baseline_models, tier_turns "
|
|
'FROM "LiteLLM_AutoRouterSession" WHERE api_key = %s AND session_id = %s AND router_name = %s',
|
|
(key_hash, session_id, router_name),
|
|
database_url=database_url,
|
|
)
|
|
assert len(rows) == 1, rows
|
|
return rows[0]
|
|
|
|
|
|
def _session_contract(session_id: str, router_name: str) -> dict[str, object]:
|
|
return {
|
|
"session_id": session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"turns": 1,
|
|
"last_model": "openai/integration-simple",
|
|
"spend": pytest.approx(0.000015),
|
|
"savings_estimated_turns": 1,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000015),
|
|
"saved_spend": pytest.approx(0.000135),
|
|
"baseline_spend": pytest.approx(0.00015),
|
|
"savings_estimated_baseline_spend": pytest.approx(0.00015),
|
|
"baseline_model": "openai/integration-baseline",
|
|
"baseline_models": {"openai/integration-baseline": 1},
|
|
}
|
|
|
|
|
|
@pytest.mark.timeout(600)
|
|
def test_auto_router_session_tracks_key_scoped_spend_and_bounds_long_session_ids(
|
|
gateway: Gateway,
|
|
tmp_path: Path,
|
|
) -> None:
|
|
normal_session_id: Final = str(uuid.uuid4())
|
|
messages_session_id: Final = str(uuid.uuid4())
|
|
responses_session_id: Final = str(uuid.uuid4())
|
|
codex_session_id: Final = str(uuid.uuid4())
|
|
codex_thread_id: Final = str(uuid.uuid4())
|
|
opencode_session_id: Final = str(uuid.uuid4())
|
|
thread_only_session_id: Final = str(uuid.uuid4())
|
|
long_session_id: Final = "s" * 300
|
|
router_name: Final = "auto-router-" + uuid.uuid4().hex[:8]
|
|
normal_prompt: Final = f"integration-auto-simple {normal_session_id}"
|
|
messages_prompt: Final = f"integration-auto-simple {messages_session_id}"
|
|
messages_second_prompt: Final = f"integration-auto-medium {messages_session_id}"
|
|
responses_prompt: Final = f"integration-auto-simple {responses_session_id}"
|
|
codex_prompt: Final = f"integration-auto-simple {codex_session_id}"
|
|
opencode_prompt: Final = f"integration-auto-simple {opencode_session_id}"
|
|
thread_only_prompt: Final = f"integration-auto-simple {thread_only_session_id}"
|
|
long_prompt: Final = f"integration-auto-simple {long_session_id}"
|
|
messages_upstream_body: Final = {
|
|
"model": "integration-simple",
|
|
"input": [
|
|
{
|
|
"type": "message",
|
|
"role": "user",
|
|
"content": [{"type": "input_text", "text": messages_prompt}],
|
|
}
|
|
],
|
|
"include": ["reasoning.encrypted_content"],
|
|
"max_output_tokens": 32,
|
|
}
|
|
messages_second_upstream_body: Final = {
|
|
"model": "integration-medium",
|
|
"input": [
|
|
{
|
|
"type": "message",
|
|
"role": "user",
|
|
"content": [{"type": "input_text", "text": messages_second_prompt}],
|
|
}
|
|
],
|
|
"include": ["reasoning.encrypted_content"],
|
|
"max_output_tokens": 32,
|
|
}
|
|
responses_upstream_body: Final = {
|
|
"model": "integration-simple",
|
|
"input": responses_prompt,
|
|
}
|
|
|
|
with (
|
|
scratch_database() as database_url,
|
|
wire_server(
|
|
_tier_peer(
|
|
"integration-simple",
|
|
(normal_prompt, codex_prompt, opencode_prompt, thread_only_prompt, long_prompt),
|
|
)
|
|
) as simple,
|
|
wire_server(_tier_peer("integration-medium", ())) as medium,
|
|
wire_server(_tier_peer("integration-complex", (normal_prompt,))) as complex_tier,
|
|
wire_server(_tier_peer("integration-baseline", (normal_prompt,))) as baseline,
|
|
):
|
|
config: Final = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())
|
|
config["model_list"] = [
|
|
_deployment("integration-simple", "integration-simple", simple, 0.000001, 0.000002),
|
|
_deployment("integration-medium", "integration-medium", medium, 0.000002, 0.000004),
|
|
_deployment("integration-complex", "integration-complex", complex_tier, 0.000004, 0.000008),
|
|
_deployment("integration-baseline", "integration-baseline", baseline, 0.00001, 0.00002),
|
|
{
|
|
"model_name": router_name,
|
|
"litellm_params": {
|
|
"model": "auto_router/complexity_router",
|
|
"complexity_router_config": {
|
|
"classifier_type": "heuristic",
|
|
"keyword_tier_rules": [
|
|
{"keywords": ["integration-auto-simple"], "tier": "SIMPLE"},
|
|
{"keywords": ["integration-auto-medium"], "tier": "MEDIUM"},
|
|
],
|
|
"tiers": {
|
|
"SIMPLE": "integration-simple",
|
|
"MEDIUM": "integration-medium",
|
|
"COMPLEX": "integration-complex",
|
|
"REASONING": "integration-baseline",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
]
|
|
config["router_settings"] = {**config["router_settings"], "num_retries": 0, "disable_cooldowns": True}
|
|
config_path: Final = tmp_path / "auto_router_management.yaml"
|
|
config_path.write_text(yaml.safe_dump(config))
|
|
|
|
with owned_proxy(gateway, tmp_path, {"DATABASE_URL": database_url}, config=config_path, workers=1) as candidate:
|
|
with candidate.scenario() as scenario:
|
|
key: Final = scenario.key()
|
|
other_key: Final = scenario.key()
|
|
response: Final = candidate.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": router_name, "messages": [{"role": "user", "content": normal_prompt}]},
|
|
key=key,
|
|
headers={"x-claude-code-session-id": normal_session_id},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert _assistant_content(response) == "synthetic tier response", response.text
|
|
|
|
session: Final = _session_response(candidate, key, normal_session_id)
|
|
assert JSON_OBJECT.validate_json(session.content) == {
|
|
"session_id": normal_session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"turns": 1,
|
|
"last_model": "openai/integration-simple",
|
|
"spend": pytest.approx(0.000015),
|
|
"savings_estimated_turns": 1,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000015),
|
|
"saved_spend": pytest.approx(0.000135),
|
|
"baseline_spend": pytest.approx(0.00015),
|
|
"savings_estimated_baseline_spend": pytest.approx(0.00015),
|
|
"baseline_model": "openai/integration-baseline",
|
|
"baseline_models": {"openai/integration-baseline": 1},
|
|
}, session.text
|
|
_assert_rollup_row(key, database_url, router_name, normal_session_id)
|
|
|
|
hidden: Final = candidate.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": normal_session_id},
|
|
key=other_key,
|
|
)
|
|
assert hidden.status_code == 404, hidden.text
|
|
assert JSON_OBJECT.validate_json(hidden.content) == {
|
|
"detail": f"No auto-routed turns recorded for session {normal_session_id!r} under this key"
|
|
}, hidden.text
|
|
|
|
messages_response: Final = candidate.client.post(
|
|
"/v1/messages",
|
|
json={
|
|
"model": router_name,
|
|
"max_tokens": 32,
|
|
"messages": [{"role": "user", "content": messages_prompt}],
|
|
},
|
|
headers={
|
|
"x-api-key": key,
|
|
"anthropic-version": "2023-06-01",
|
|
"x-claude-code-session-id": messages_session_id,
|
|
},
|
|
)
|
|
assert messages_response.status_code == 200, messages_response.text
|
|
assert JSON_OBJECT.validate_json(messages_response.content)["content"] == [
|
|
{"type": "text", "text": "synthetic tier response"}
|
|
], messages_response.text
|
|
|
|
messages_second_response: Final = candidate.client.post(
|
|
"/v1/messages",
|
|
json={
|
|
"model": router_name,
|
|
"max_tokens": 32,
|
|
"messages": [{"role": "user", "content": messages_second_prompt}],
|
|
},
|
|
headers={
|
|
"x-api-key": key,
|
|
"anthropic-version": "2023-06-01",
|
|
"x-claude-code-session-id": messages_session_id,
|
|
},
|
|
)
|
|
assert messages_second_response.status_code == 200, messages_second_response.text
|
|
assert JSON_OBJECT.validate_json(messages_second_response.content)["content"] == [
|
|
{"type": "text", "text": "synthetic tier response"}
|
|
], messages_second_response.text
|
|
|
|
def messages_session_settled() -> None:
|
|
session: Final = candidate.request(
|
|
"GET", "/auto_router/session", params={"session_id": messages_session_id}, key=key
|
|
)
|
|
assert JSON_OBJECT.validate_json(session.content) == {
|
|
"session_id": messages_session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"turns": 2,
|
|
"last_model": "openai/integration-medium",
|
|
"spend": pytest.approx(0.000045),
|
|
"savings_estimated_turns": 2,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000045),
|
|
"saved_spend": pytest.approx(0.000255),
|
|
"baseline_spend": pytest.approx(0.0003),
|
|
"savings_estimated_baseline_spend": pytest.approx(0.0003),
|
|
"baseline_model": "openai/integration-baseline",
|
|
"baseline_models": {"openai/integration-baseline": 2},
|
|
}, session.text
|
|
assert _rollup_row(key, database_url, router_name, messages_session_id) == {
|
|
"session_id": messages_session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"last_model": "openai/integration-medium",
|
|
"turns": 2,
|
|
"spend": pytest.approx(0.000045),
|
|
"saved_spend": pytest.approx(0.000255),
|
|
"savings_estimated_turns": 2,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000045),
|
|
"savings_estimated_saved_spend": pytest.approx(0.000255),
|
|
"savings_estimated_baseline_models": {"openai/integration-baseline": 2},
|
|
"baseline_models": {"openai/integration-baseline": 2},
|
|
"tier_turns": {"SIMPLE": 1, "MEDIUM": 1},
|
|
}
|
|
|
|
until_asserted(messages_session_settled, seconds=70)
|
|
|
|
responses_response: Final = candidate.client.post(
|
|
"/v1/responses",
|
|
json={"model": router_name, "input": responses_prompt},
|
|
headers={
|
|
"Authorization": f"Bearer {key}",
|
|
"user-agent": "codex_cli_rs/0.50.0",
|
|
"session-id": responses_session_id,
|
|
"thread-id": str(uuid.uuid4()),
|
|
},
|
|
)
|
|
assert responses_response.status_code == 200, responses_response.text
|
|
responses_output: Final = JSON_ARRAY.validate_python(
|
|
JSON_OBJECT.validate_json(responses_response.content)["output"]
|
|
)
|
|
assert len(responses_output) == 1, responses_response.text
|
|
assert object_value(responses_output[0])["content"] == [
|
|
{
|
|
"type": "output_text",
|
|
"text": "synthetic tier response",
|
|
"annotations": [],
|
|
"logprobs": None,
|
|
}
|
|
], responses_response.text
|
|
responses_session: Final = _session_response(candidate, key, responses_session_id)
|
|
assert JSON_OBJECT.validate_json(responses_session.content) == _session_contract(
|
|
responses_session_id, router_name
|
|
), responses_session.text
|
|
_assert_rollup_row(key, database_url, router_name, responses_session_id)
|
|
|
|
codex_response: Final = candidate.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": router_name, "messages": [{"role": "user", "content": codex_prompt}]},
|
|
key=key,
|
|
headers={
|
|
"user-agent": "codex_cli_rs/0.50.0",
|
|
"session-id": codex_session_id,
|
|
"thread-id": codex_thread_id,
|
|
},
|
|
)
|
|
assert codex_response.status_code == 200, codex_response.text
|
|
assert _assistant_content(codex_response) == "synthetic tier response", codex_response.text
|
|
codex_session: Final = _session_response(candidate, key, codex_session_id)
|
|
assert JSON_OBJECT.validate_json(codex_session.content) == _session_contract(
|
|
codex_session_id, router_name
|
|
), codex_session.text
|
|
_assert_rollup_row(key, database_url, router_name, codex_session_id)
|
|
codex_thread: Final = candidate.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": codex_thread_id},
|
|
key=key,
|
|
)
|
|
assert codex_thread.status_code == 404, codex_thread.text
|
|
assert JSON_OBJECT.validate_json(codex_thread.content) == {
|
|
"detail": f"No auto-routed turns recorded for session {codex_thread_id!r} under this key"
|
|
}, codex_thread.text
|
|
codex_hidden: Final = candidate.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": codex_session_id},
|
|
key=other_key,
|
|
)
|
|
assert codex_hidden.status_code == 404, codex_hidden.text
|
|
assert JSON_OBJECT.validate_json(codex_hidden.content) == {
|
|
"detail": f"No auto-routed turns recorded for session {codex_session_id!r} under this key"
|
|
}, codex_hidden.text
|
|
|
|
opencode_response: Final = candidate.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": router_name, "messages": [{"role": "user", "content": opencode_prompt}]},
|
|
key=key,
|
|
headers={
|
|
"x-session-id": opencode_session_id,
|
|
"user-agent": "opencode/0.15.0",
|
|
},
|
|
)
|
|
assert opencode_response.status_code == 200, opencode_response.text
|
|
assert _assistant_content(opencode_response) == "synthetic tier response", opencode_response.text
|
|
opencode_session: Final = _session_response(
|
|
candidate,
|
|
key,
|
|
opencode_session_id,
|
|
return_last_on_timeout=True,
|
|
)
|
|
assert JSON_OBJECT.validate_json(opencode_session.content) == _session_contract(
|
|
opencode_session_id, router_name
|
|
), opencode_session.text
|
|
_assert_rollup_row(key, database_url, router_name, opencode_session_id)
|
|
opencode_hidden: Final = candidate.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": opencode_session_id},
|
|
key=other_key,
|
|
)
|
|
assert opencode_hidden.status_code == 404, opencode_hidden.text
|
|
assert JSON_OBJECT.validate_json(opencode_hidden.content) == {
|
|
"detail": f"No auto-routed turns recorded for session {opencode_session_id!r} under this key"
|
|
}, opencode_hidden.text
|
|
|
|
thread_only_response: Final = candidate.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": router_name, "messages": [{"role": "user", "content": thread_only_prompt}]},
|
|
key=key,
|
|
headers={
|
|
"user-agent": "codex_cli_rs/0.50.0",
|
|
"thread-id": thread_only_session_id,
|
|
},
|
|
)
|
|
assert thread_only_response.status_code == 200, thread_only_response.text
|
|
assert _assistant_content(thread_only_response) == "synthetic tier response", thread_only_response.text
|
|
thread_only_session: Final = _session_response(candidate, key, thread_only_session_id)
|
|
assert JSON_OBJECT.validate_json(thread_only_session.content) == _session_contract(
|
|
thread_only_session_id, router_name
|
|
), thread_only_session.text
|
|
_assert_rollup_row(key, database_url, router_name, thread_only_session_id)
|
|
thread_only_hidden: Final = candidate.request(
|
|
"GET",
|
|
"/auto_router/session",
|
|
params={"session_id": thread_only_session_id},
|
|
key=other_key,
|
|
)
|
|
assert thread_only_hidden.status_code == 404, thread_only_hidden.text
|
|
assert JSON_OBJECT.validate_json(thread_only_hidden.content) == {
|
|
"detail": f"No auto-routed turns recorded for session {thread_only_session_id!r} under this key"
|
|
}, thread_only_hidden.text
|
|
|
|
long_response: Final = candidate.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": router_name, "messages": [{"role": "user", "content": long_prompt}]},
|
|
key=key,
|
|
headers={"x-litellm-session-id": long_session_id},
|
|
)
|
|
assert long_response.status_code == 200, long_response.text
|
|
assert _assistant_content(long_response) == "synthetic tier response", long_response.text
|
|
|
|
bounded_session_id: Final = "sha256:" + hashlib.sha256(long_session_id.encode()).hexdigest()
|
|
long_session: Final = _session_response(
|
|
candidate,
|
|
key,
|
|
long_session_id,
|
|
database_url,
|
|
bounded_session_id,
|
|
seconds=70,
|
|
return_last_on_timeout=True,
|
|
)
|
|
assert JSON_OBJECT.validate_json(long_session.content) == {
|
|
"session_id": long_session_id,
|
|
"router_name": router_name,
|
|
"router_type": "complexity",
|
|
"turns": 1,
|
|
"last_model": "openai/integration-simple",
|
|
"spend": pytest.approx(0.000015),
|
|
"savings_estimated_turns": 1,
|
|
"savings_estimated_actual_spend": pytest.approx(0.000015),
|
|
"saved_spend": pytest.approx(0.000135),
|
|
"baseline_spend": pytest.approx(0.00015),
|
|
"savings_estimated_baseline_spend": pytest.approx(0.00015),
|
|
"baseline_model": "openai/integration-baseline",
|
|
"baseline_models": {"openai/integration-baseline": 1},
|
|
}, long_session.text
|
|
_assert_rollup_row(key, database_url, router_name, bounded_session_id)
|
|
|
|
simple_calls: Final = tuple(request for request in simple.drain() if request.method == "POST")
|
|
assert tuple(
|
|
(
|
|
request.method,
|
|
request.target,
|
|
json.loads(request.body),
|
|
)
|
|
for request in simple_calls
|
|
) == (
|
|
(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": "integration-simple", "messages": [{"role": "user", "content": normal_prompt}]},
|
|
),
|
|
("POST", "/v1/responses", messages_upstream_body),
|
|
("POST", "/v1/responses", responses_upstream_body),
|
|
(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": "integration-simple", "messages": [{"role": "user", "content": codex_prompt}]},
|
|
),
|
|
(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": "integration-simple", "messages": [{"role": "user", "content": opencode_prompt}]},
|
|
),
|
|
(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": "integration-simple", "messages": [{"role": "user", "content": thread_only_prompt}]},
|
|
),
|
|
(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": "integration-simple", "messages": [{"role": "user", "content": long_prompt}]},
|
|
),
|
|
), response.text
|
|
medium_calls: Final = tuple(request for request in medium.drain() if request.method == "POST")
|
|
assert tuple((request.method, request.target, json.loads(request.body)) for request in medium_calls) == (
|
|
("POST", "/v1/responses", messages_second_upstream_body),
|
|
), response.text
|
|
assert tuple(request for request in complex_tier.drain() if request.method == "POST") == (), response.text
|
|
assert tuple(request for request in baseline.drain() if request.method == "POST") == (), response.text
|