test(e2e): cover /openai chat passthrough cost logging (#34470)

The /openai/{endpoint} passthrough forwards a raw OpenAI-format request to
api.openai.com (or OPENAI_API_BASE) with the proxy's OPENAI_API_KEY swapped in,
and still logs a costed pass_through_endpoint SpendLogs row. Nothing exercised
that path end to end.

Adds a live /openai/v1/chat/completions passthrough test that asserts a 2xx
completion and a costed row with custom_llm_provider=openai, mirroring the
gemini and anthropic passthrough cost tests, and registers
llm.chat_completions.openai.passthrough.nonstream.cost_logged.
This commit is contained in:
mubashir1osmani 2026-07-25 10:54:28 -07:00 committed by GitHub
parent 6cc136de90
commit 8ce365511f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View file

@ -2,6 +2,7 @@
- {id: llm.chat_completions.openai.basic.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: basic, streaming: nonstream, assertions: [works], source: "proxy_server.py:8455", rationale: "Core endpoint/route/capability"}
- {id: llm.chat_completions.openai.basic.stream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: basic, streaming: stream, assertions: [works], source: "proxy_server.py:8455", rationale: "Core streaming"}
- {id: llm.chat_completions.openai.basic.nonstream.cost_logged, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: basic, streaming: nonstream, assertions: [works, cost_logged], source: "proxy_server.py:8455", rationale: "Cost logging regression catch"}
- {id: llm.chat_completions.openai.passthrough.nonstream.cost_logged, module: llm, tier: P1, subject_endpoint: chat_completions, route: openai, capability: basic, streaming: nonstream, assertions: [works, cost_logged], source: "test_passthrough_e2e.py", rationale: "OpenAI-format chat via the raw /openai/{endpoint} passthrough (/openai/v1/chat/completions); proxy swaps in OPENAI_API_KEY and still logs a costed pass_through_endpoint row (LIT-4752)"}
- {id: llm.chat_completions.openai.tool_use.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: tool_use, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "OpenAI function_calling; high usage"}
- {id: llm.chat_completions.openai.tool_use.stream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: tool_use, streaming: stream, assertions: [works], source: "model_prices json", rationale: "Tool calls over streaming"}
- {id: llm.chat_completions.openai.vision.nonstream.works, module: llm, tier: P0, subject_endpoint: chat_completions, route: openai, capability: vision, streaming: nonstream, assertions: [works], source: "model_prices json", rationale: "gpt-4o vision; high usage"}

View file

@ -102,6 +102,12 @@ class AnthropicMessageBody(BaseModel):
stream: bool = False
class OpenAIChatBody(BaseModel):
model: str
messages: list[ChatMessage]
max_tokens: int = 64
class VllmChatBody(BaseModel):
model: str
messages: list[ChatMessage]
@ -191,6 +197,19 @@ class PassthroughClient:
stream=stream,
)
def openai_chat(
self, key: str, model: str, text: str, *, max_tokens: int = 64
) -> StreamingResponse:
return self.proxy.transport.send(
"/openai/v1/chat/completions",
headers=self.proxy.transport.bearer(key),
json=OpenAIChatBody(
model=model,
max_tokens=max_tokens,
messages=[ChatMessage(role="user", content=text)],
),
)
def vllm_chat(
self, key: str, model: str, text: str, *, max_tokens: int = 64
) -> StreamingResponse:

View file

@ -160,6 +160,18 @@ def test_anthropic_passthrough_tool_call_logs_cost(
assert row.custom_llm_provider == "anthropic"
@pytest.mark.covers("llm.chat_completions.openai.passthrough.nonstream.cost_logged")
def test_openai_passthrough_nonstreaming_logs_cost(
client: PassthroughClient, scoped_key: str
) -> None:
result = client.openai_chat(scoped_key, "gpt-5.4-mini", "Say hello in one word")
require_successful_call(result)
row = _fetch_cost_breakdown(client, result)
assert row.custom_llm_provider == "openai"
assert "gpt-5" in (row.model or "")
class TestPassthroughModelAllowlist:
"""A passthrough route must honor the calling key's model allow-list.