litellm/tests/integration/routing/test_azure_passthrough_spend_row.py
devin-ai-integration[bot] 2d4472efc1
fix(vllm): write spend log rows for /vllm passthrough calls to router models (#45733)
* fix(vllm): write spend log rows for /vllm passthrough calls to router models

The vLLM passthrough config never built a logged response for relayed calls, so stream and non-stream calls through /vllm with a router model wrote no spend row and never charged the key. vLLM now shares the OpenAI-shaped relay logging that Azure already had, moved into an openai_like helper both configs call.

* ci: run the vllm passthrough unit tests in the llms-providers shard

* fix(proxy): log /vllm text completions and ask router models for stream usage

A chat stream relayed to a router model without stream_options now gets
include_usage added on the way out, and the usage chunk vLLM appends is
removed before the client sees it, so the spend row carries vLLM's own
token counts instead of a local estimate. /vllm/completions calls now
write a row too, stream and non-stream.

* fix(vllm): keep usage, fingerprints, and disconnect rows correct on /vllm streams

Inject and strip stream usage on /vllm/completions too, hold every finish
frame for the fingerprint when n > 1, pass a forced-usage server's stream
through untouched, stop turning continuous_usage_stats alone into per-chunk
usage, close the upstream when the client hangs up so the row is written,
and assemble text choices with one sort instead of a scan per choice

* fix(vllm): keep n>1 stream order when holding finish chunks for the fingerprint

A held finish chunk now goes out as soon as any other chunk arrives, so a
choice that finishes while another still streams keeps its place. Only the
finish chunks right before the usage chunk get vLLM's system_fingerprint.

* fix(vllm): keep caller stream_options and log both success handlers

Forward a stream_options the proxy cannot read, or a non-boolean
include_usage, to vLLM as the caller sent it. Only a false
always_include_stream_usage changes the /vllm relay, so clients get the
same stream bytes under every setting.

Record the passthrough origin when logging starts so the sync and async
success handlers both parse the relayed body, hand callbacks a parsed
text completion, and read relayed messages and prompt separately so a
stray field no longer drops the local count.

Add the audit's integration tests for the relay, the settings, and the
Azure spend row.

* test(vllm): expect the caller's stream under always_include_stream_usage true

* fix(vllm): relay long SSE frames in linear time

* test(vllm): time the relay's usage and finish_reason prefilter on one large frame

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-14 12:40:48 -07:00

228 lines
9.3 KiB
Python

import json
import uuid
from collections.abc import Callable, Iterator, Mapping, Sequence
from contextlib import contextmanager
from typing import Final
from urllib.parse import urlsplit
import pytest
from integration._support.client import JSON_OBJECT, Gateway, held
from integration._support.database import read_rows
from integration._support.openai_wire import answering_model_discovery, responses_reply
from integration._support.wire import Reply, Request, Wire, wire_server
from pydantic import JsonValue
_DEPLOYMENT: Final = "relay-deployment"
_API_VERSION: Final = "2024-10-21"
_INPUT_COST: Final = 0.001
_OUTPUT_COST: Final = 0.002
_USAGE: Final[dict[str, JsonValue]] = {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8}
def _marker() -> str:
return "azure-relay-" + uuid.uuid4().hex
def _frame(payload: Mapping[str, JsonValue] | str) -> bytes:
return b"data: " + (payload if isinstance(payload, str) else json.dumps(payload)).encode() + b"\n\n"
def _chunk(identity: str, choices: Sequence[JsonValue], **extra: JsonValue) -> dict[str, JsonValue]:
return {
"id": identity,
"object": "chat.completion.chunk",
"created": 1,
"model": "gpt",
"choices": list(choices),
**extra,
}
def _chat_stream(identity: str, with_usage: bool) -> tuple[bytes, ...]:
return (
_frame(_chunk(identity, [{"index": 0, "delta": {"content": "Hello there"}, "finish_reason": None}])),
_frame(_chunk(identity, [{"index": 0, "delta": {}, "finish_reason": "stop"}])),
*((_frame(_chunk(identity, [], usage=_USAGE)),) if with_usage else ()),
_frame("[DONE]"),
)
def _chat_completion(identity: str) -> bytes:
return json.dumps(
{
"id": identity,
"object": "chat.completion",
"created": 1,
"model": "gpt",
"choices": [{"index": 0, "message": {"role": "assistant", "content": "Hello"}, "finish_reason": "stop"}],
"usage": _USAGE,
}
).encode()
def _embedding() -> bytes:
return json.dumps(
{
"object": "list",
"data": [{"object": "embedding", "index": 0, "embedding": [0.25, 0.5]}],
"model": "embed",
"usage": {"prompt_tokens": 5, "total_tokens": 5},
}
).encode()
def _asks_usage(body: Mapping[str, JsonValue]) -> bool:
options: Final = body.get("stream_options")
return isinstance(options, dict) and options.get("include_usage") is True
def _respond(marker: str) -> Callable[[Request], Reply]:
identity: Final = f"chatcmpl-{marker}"
def respond(request: Request) -> Reply:
body: Final = JSON_OBJECT.validate_json(request.body)
assert marker in json.dumps(body), body
path: Final = urlsplit(request.target).path
if path.endswith("/embeddings"):
return Reply(body=_embedding())
if path.endswith("/responses"):
return responses_reply(f"resp_{marker}", "gpt", "Hello", stream=body.get("stream") is True)
if body.get("stream") is True:
return Reply(content_type="text/event-stream", chunks=_chat_stream(identity, _asks_usage(body)))
return Reply(body=_chat_completion(identity))
return answering_model_discovery(respond)
@contextmanager
def _deployment(gateway: Gateway, marker: str, provider: str) -> Iterator[tuple[Wire, str]]:
with wire_server(_respond(marker)) as wire, gateway.scenario() as scenario:
model: Final = scenario.model(
model=f"{provider}/{_DEPLOYMENT}",
api_base=wire.url,
api_key="sk-azure-relay",
api_version=_API_VERSION,
input_cost_per_token=_INPUT_COST,
output_cost_per_token=_OUTPUT_COST,
)
yield wire, model
def _posted(wire: Wire) -> tuple[tuple[str, dict[str, JsonValue]], ...]:
return tuple(
(urlsplit(request.target).path, JSON_OBJECT.validate_json(request.body))
for request in wire.drain()
if request.method == "POST"
)
def _one_row(model: str) -> dict[str, JsonValue]:
rows: Final = held(
lambda: read_rows(
"SELECT request_id, status, custom_llm_provider, prompt_tokens, completion_tokens, spend "
'FROM "LiteLLM_SpendLogs" WHERE model_group=%s',
(model,),
),
lambda found: len(found) >= 1,
holding=2,
seconds=70,
)
assert len(rows) == 1, rows
return rows[0]
def _assert_row(row: Mapping[str, JsonValue], provider: str, prompt: int, completion: int) -> None:
assert (row["status"], row["custom_llm_provider"]) == ("success", provider), row
assert (row["prompt_tokens"], row["completion_tokens"]) == (prompt, completion), row
assert row["spend"] == pytest.approx(prompt * _INPUT_COST + completion * _OUTPUT_COST), row
def _chat_path(model: str, route: str) -> str:
return (
f"/azure/openai/deployments/{model}/chat/completions?api-version={_API_VERSION}"
if route == "azure"
else f"/azure_ai/{model}/chat/completions"
)
def _chat_body(marker: str, **extra: JsonValue) -> dict[str, JsonValue]:
return {"messages": [{"role": "user", "content": marker}], **extra}
@pytest.mark.parametrize("provider", ["azure", "azure_ai"])
def test_router_model_chat_non_stream_writes_the_upstream_usage(gateway: Gateway, provider: str) -> None:
marker: Final = _marker()
with _deployment(gateway, marker, provider) as (wire, model):
response: Final = gateway.request("POST", _chat_path(model, provider), _chat_body(marker))
assert response.status_code == 200, response.text
assert response.content == _chat_completion(f"chatcmpl-{marker}"), response.text
((path, _),) = _posted(wire)
assert path.endswith("/chat/completions"), path
row: Final = _one_row(model)
assert row["request_id"] == f"chatcmpl-{marker}", row
_assert_row(row, provider, 5, 3)
@pytest.mark.parametrize("provider", ["azure", "azure_ai"])
def test_router_model_chat_stream_with_usage_is_relayed_verbatim_and_logged(gateway: Gateway, provider: str) -> None:
marker: Final = _marker()
with _deployment(gateway, marker, provider) as (wire, model):
sent: Final = _chat_body(marker, stream=True, stream_options={"include_usage": True})
response: Final = gateway.request("POST", _chat_path(model, provider), sent)
assert response.status_code == 200, response.text
assert response.content == b"".join(_chat_stream(f"chatcmpl-{marker}", True)), response.text
((_, posted),) = _posted(wire)
assert posted == sent, posted
_assert_row(_one_row(model), provider, 5, 3)
@pytest.mark.parametrize(
"extra",
[pytest.param({}, id="plain"), pytest.param({"prompt": 5}, id="stray_prompt_field")],
)
def test_azure_chat_stream_without_usage_is_forwarded_untouched_and_counted_from_the_messages(
gateway: Gateway, extra: Mapping[str, JsonValue]
) -> None:
marker: Final = _marker()
with _deployment(gateway, marker, "azure") as (wire, model):
sent: Final = _chat_body(marker, stream=True, **extra)
response: Final = gateway.request("POST", _chat_path(model, "azure"), sent)
assert response.status_code == 200, response.text
assert response.content == b"".join(_chat_stream(f"chatcmpl-{marker}", False)), response.text
((_, posted),) = _posted(wire)
assert posted == sent, posted
row: Final = _one_row(model)
prompt, completion = row["prompt_tokens"], row["completion_tokens"]
assert row["status"] == "success", row
assert isinstance(prompt, int) and prompt > 0, row
assert isinstance(completion, int) and completion > 0, row
assert row["spend"] == pytest.approx(prompt * _INPUT_COST + completion * _OUTPUT_COST), row
@pytest.mark.parametrize("stream", [False, True], ids=["non_stream", "stream"])
def test_azure_responses_body_model_writes_the_upstream_usage(gateway: Gateway, stream: bool) -> None:
marker: Final = _marker()
with _deployment(gateway, marker, "azure") as (wire, model):
sent: Final = {"model": model, "input": marker, **({"stream": True} if stream else {})}
response: Final = gateway.request("POST", f"/azure/openai/responses?api-version={_API_VERSION}", sent)
assert response.status_code == 200, response.text
assert f"resp_{marker}" in response.text, response.text
((path, _),) = _posted(wire)
assert path.endswith("/responses"), path
_assert_row(_one_row(model), "azure", 5, 3)
def test_azure_embeddings_write_the_upstream_prompt_tokens(gateway: Gateway) -> None:
marker: Final = _marker()
with _deployment(gateway, marker, "azure") as (wire, model):
response: Final = gateway.request(
"POST", f"/azure/openai/deployments/{model}/embeddings?api-version={_API_VERSION}", {"input": marker}
)
assert response.status_code == 200, response.text
assert response.content == _embedding(), response.text
((path, _),) = _posted(wire)
assert path == f"/openai/deployments/{_DEPLOYMENT}/embeddings", path
row: Final = _one_row(model)
assert (row["status"], row["custom_llm_provider"]) == ("success", "azure"), row
assert (row["prompt_tokens"], row["completion_tokens"]) == (5, 0), row
assert row["spend"] == pytest.approx(5 * _INPUT_COST), row