Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
from __future__ import annotations
import asyncio
import base64
import json
from collections.abc import Mapping
from dataclasses import replace
from typing import Any
import httpx
import pytest
import core.agent_core.ai._common as common_module
import core.agent_core.ai.anthropic as anthropic_module
import core.agent_core.ai.sse as sse_module
import core.agent_core.ai.openai_responses as responses_module
from core.agent_core.ai.anthropic import AnthropicAdapter, build_messages_payload
from core.agent_core.ai._common import OutputBudgetExceeded, ServedHop, StreamAssembler, read_response_body
from core.agent_core.ai.openai_chat import OpenAIChatAdapter, build_chat_payload
from core.agent_core.ai.openai_responses import OpenAIResponsesAdapter, build_responses_payload
from core.agent_core.ai.provider import (
BlockEnd,
Done,
MalformedToolCall,
MediaLoader,
ModelEndpoint,
ModelRequest,
ProviderError,
TextDelta,
ThinkingDelta,
ToolCallDelta,
ToolCallStart,
)
from core.agent_core.ai.registry import ADAPTERS, adapter_class, sent_effort
from core.agent_core.messages import PROTOCOLS
from tests.agent_core.ai._wire import build_wire_request
from core.agent_core.cancel import CancelToken
from core.agent_core.messages import (
AssistantMessage,
ImageBlock,
Origin,
TextBlock,
ThinkingBlock,
ToolCallBlock,
ToolResultMessage,
UserMessage,
Usage,
)
from core.agent_core.tools.base import ToolSpec
from tests.agent_core.fakes import WINDOW_REFUSAL, anthropic_error, model_hub_error
def _request(
protocol: str,
*,
messages: tuple[Any, ...] = (),
supports_images: bool = False,
tools: tuple[ToolSpec, ...] = (),
model_id: str = "model-x",
base_url: str = "https://model.test/v1",
reasoning_effort: str | None = "high",
) -> ModelRequest:
return ModelRequest(
endpoint=ModelEndpoint(
protocol,
base_url,
model_id,
"token",
provider={"anthropic": "anthropic", "openai_chat": "openai", "openai_responses": "openai"}[protocol],
),
system="system",
messages=messages or (UserMessage((TextBlock(text="hello"),)),),
tools=tools,
max_tokens=128,
reasoning_effort=reasoning_effort,
supports_images=supports_images,
)
@pytest.mark.parametrize(
("cache_ttl", "cache_ttl_wire", "expected"),
[
(None, False, None),
("12h", True, {"ttl": "12h"}),
("30m", False, None), # relays may keep the local estimate, never OpenAI's wire option
],
)
def test_responses_prompt_cache_retention_is_wired_only_when_model_hub_confirms_it(
cache_ttl: str | None, cache_ttl_wire: bool, expected: dict[str, str] | None,
) -> None:
request = _request("openai_responses", model_id="gpt-6-astra")
request = replace(
request,
endpoint=replace(request.endpoint, cache_ttl=cache_ttl, cache_ttl_wire=cache_ttl_wire),
)
payload = build_responses_payload(request, request.messages)
assert payload.get("prompt_cache_options") == expected
def test_registry_has_an_adapter_for_every_protocol() -> None:
assert set(ADAPTERS) == set(PROTOCOLS)
with pytest.raises(ValueError, match="unsupported provider protocol: bedrock"):
adapter_class("bedrock") # type: ignore[arg-type]
def test_each_known_wire_event_has_a_declarative_shape_row() -> None:
assert set(anthropic_module._KNOWN_STREAM_EVENTS) <= set(anthropic_module._ANTHROPIC_WIRE_SHAPES)
assert set(responses_module._KNOWN_RESPONSE_EVENTS) <= set(responses_module._RESPONSES_WIRE_SHAPES)
async def _events(adapter: Any, request: ModelRequest, cancel: CancelToken | None = None) -> list[Any]:
return [event async for event in adapter.stream(request, cancel or CancelToken())]
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
@pytest.mark.parametrize("header", [None, "X-Portkey-Api-Key", "x-litellm-api-key"])
async def test_configured_literal_credentials_cross_the_real_adapter_boundary(
protocol: str, header: str | None,
) -> None:
# No recognizable key/scheme/shape in the exception: only configured literals
# can protect the diagnostic emitted by a rejecting transport.
secret = "opaque.private~value\n"
request = _request(protocol)
request = replace(request, endpoint=replace(
request.endpoint,
token=secret if header is None else "token",
request_headers={} if header is None else {header: secret},
))
def handler(_: httpx.Request) -> httpx.Response:
raise httpx.LocalProtocolError(f"Illegal header value {secret.encode()!r}")
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_class(protocol)(client), request)
error = events[-1]
assert isinstance(error, ProviderError)
assert error.message == "Illegal header value b'[redacted]'"
@pytest.mark.asyncio
@pytest.mark.parametrize("stage", ["open", "closed", "terminal-backfill"])
async def test_responses_reasoning_state_has_one_serialization_owner(
monkeypatch: pytest.MonkeyPatch, stage: str,
) -> None:
item = {"type": "reasoning", "id": "rs", "encrypted_content": "ENC"}
initial = item if stage != "terminal-backfill" else {"type": "reasoning", "id": "rs"}
frames = [{"type": "response.output_item.added", "output_index": 0, "item": initial}]
if stage != "open":
frames.append({"type": "response.output_item.done", "output_index": 0, "item": initial})
if stage != "terminal-backfill":
frames.extend({
"type": "response.output_item.added" if stage == "open" else "response.output_item.done",
"output_index": 0,
# Closed items must ignore changes, not just identical snapshots.
"item": {"type": "reasoning", "id": "rs", **({"noise": str(i)} if stage == "closed" else {})},
} for i in range(20))
frames.append({"type": "response.completed", "response": {"status": "completed", "output": [item]}})
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
dumps = json.dumps
encoded_reasoning = []
def count_reasoning(value: Any, *args: Any, **kwargs: Any) -> str:
if isinstance(value, dict) and value.get("type") == "reasoning":
encoded_reasoning.append(value.copy())
return dumps(value, *args, **kwargs)
monkeypatch.setattr(json, "dumps", count_reasoning)
async with httpx.AsyncClient(transport=httpx.MockTransport(
lambda _: httpx.Response(200, text=body),
)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
block = events[-1].message.content[0]
assert isinstance(block, ThinkingBlock)
assert json.loads(block.signature)["encrypted_content"] == "ENC"
assert len(encoded_reasoning) == (2 if stage == "terminal-backfill" else 1)
def test_signature_fragments_do_not_materialize_thinking_text(monkeypatch: pytest.MonkeyPatch) -> None:
assembler = StreamAssembler(origin=Origin("p", "anthropic", "m"), protocol="anthropic")
assembler.thinking_delta(0, "long thinking")
def unexpected(_: int) -> str:
pytest.fail("signature append copied unrelated thinking text")
with monkeypatch.context() as patch:
patch.setattr(assembler, "_thinking_value", unexpected)
for _ in range(20):
assembler.append_thinking_signature(0, "s")
message = assembler.finalize("stop")
assert message.content == (ThinkingBlock("long thinking", "s" * 20),)
@pytest.mark.parametrize(
("event_name", "payload", "expected"),
[
("response.done", {}, "malformed"),
("response.done", {"type": None}, "malformed"),
("response.output_text.delta", {"type": "response.completed"}, "response.completed"),
("future", {"type": None}, None),
("future", {"type": 5}, None),
],
)
def test_dispatch_discriminator_contract(event_name: str, payload: dict, expected: str | None) -> None:
kwargs = dict(
known=responses_module._KNOWN_RESPONSE_EVENTS,
event_name=event_name,
aliases={"response.done": "response.completed"},
)
if expected == "malformed":
with pytest.raises(common_module.WireDispatchError):
common_module.dispatch_wire_event(payload, **kwargs)
else:
assert common_module.dispatch_wire_event(payload, **kwargs) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("body", [
"event: error\ndata: Overloaded\n\n",
'event: error\ndata: {"error":{"type":"overloaded_error","message":"Overloaded"}}\n\n',
'event: error\ndata: {"type":null,"error":{"type":"overloaded_error"}}\n\n',
])
async def test_anthropic_named_error_uses_pi_name_first_handling(body: str) -> None:
async with httpx.AsyncClient(transport=httpx.MockTransport(
lambda _: httpx.Response(200, text=body),
)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "overloaded"
assert events[-1].retryable is True
@pytest.mark.asyncio
async def test_anthropic_ignores_named_ping_without_parsing_payload() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":"kept"}}\n\n'
'event: ping\ndata: {}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("kept"),)
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
@pytest.mark.parametrize("source", ["http", "stream"])
async def test_long_error_diagnostics_are_redacted_once_without_recursive_rescanning(
monkeypatch: pytest.MonkeyPatch, protocol: str, source: str,
) -> None:
diagnostic = "<html><style>" + "".join(f"a{i}:b;" for i in range(1200)) + "</style>Service Unavailable</html>"
calls = []
original = common_module.redact_provider_error
def record(error: ProviderError, **kwargs: Any) -> ProviderError:
calls.append(error)
return original(error, **kwargs)
monkeypatch.setattr(common_module, "redact_provider_error", record)
if source == "http":
status, body = 503, diagnostic
else:
status = 200
body = "data: " + json.dumps({
"type": "error", "error": {"type": "api_error", "message": diagnostic},
}) + "\n\n"
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(status, text=body))) as client:
events = await _events(adapter_class(protocol)(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is True
assert error.status == (503 if source == "http" else None)
assert error.message == diagnostic
assert len(calls) == 1
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
@pytest.mark.parametrize("source", ["authorization_assignment", "relative_url", "bare_url_password"])
async def test_adapter_diagnostics_redact_assignments_and_endpoint_credentials(
protocol: str, source: str,
) -> None:
request = _request(protocol)
if source == "authorization_assignment":
secret = "dXNlcjpwYXNz"
request = replace(request, endpoint=replace(
request.endpoint, request_headers={"Authorization": "Basic " + secret},
))
diagnostic = f"upstream proxy rejected {{Authorization=Basic {secret}}}"
elif source == "relative_url":
secret = "OPAQUEKEYVALUE9"
request = replace(request, endpoint=replace(request.endpoint, base_url=f"https://gw.example/v1?key={secret}"))
diagnostic = f"no route for /v1/chat/completions?key={secret}"
else:
secret = "opaquepassword9"
request = replace(request, endpoint=replace(request.endpoint, base_url=f"https://user:{secret}@gw.example/v1"))
diagnostic = f"connection failed for user:{secret}@gw.example"
def handler(_: httpx.Request) -> httpx.Response:
if source == "authorization_assignment":
raise httpx.LocalProtocolError(diagnostic)
return httpx.Response(404, text=diagnostic)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_class(protocol)(client), request)
error = events[-1]
assert isinstance(error, ProviderError)
assert secret not in error.message
assert "[redacted]" in error.message
@pytest.mark.asyncio
@pytest.mark.parametrize(("reason", "stop"), [("max_output_tokens", "length"), ("content_filter", "safety")])
async def test_responses_interrupted_pending_identity_preserves_provider_stop(reason: str, stop: str) -> None:
frames = [
{"type": "response.output_item.added", "output_index": 0, "item": {"type": "message"}},
{"type": "response.output_text.delta", "output_index": 0, "delta": "kept"},
{"type": "response.output_item.added", "output_index": 1,
"item": {"type": "function_call", "id": "fc_1", "name": "read"}},
{"type": "response.function_call_arguments.delta", "output_index": 1, "delta": '{"path":'},
{"type": "response.incomplete", "response": {"incomplete_details": {"reason": reason}}},
]
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
message = events[-1].message
assert message.stop_reason == stop
assert message.content[0] == TextBlock("kept")
assert message.tool_calls[0].id == next(event.id for event in events if isinstance(event, ToolCallStart))
assert message.tool_calls[0].name == "read"
assert message.tool_calls[0].arguments == {}
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
async def test_empty_wire_states_consume_a_realistic_structural_budget(monkeypatch: pytest.MonkeyPatch, protocol: str) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 8192)
frames = []
for index in range(100):
if protocol == "anthropic":
frame = {"type": "content_block_start", "index": index, "content_block": {"type": "text"}}
elif protocol == "openai_chat":
frame = {"choices": [{"delta": {"tool_calls": [{"index": index, "function": {}}]}}]}
else:
frame = {"type": "response.output_item.added", "output_index": index, "item": {"type": "message"}}
frames.append(f"data: {json.dumps(frame)}\n\n")
body = "".join(frames)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(adapter_class(protocol)(client), _request(protocol))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "invalid_request"
assert "output budget" in events[-1].message
def test_tool_id_allocation_has_linear_lookup_cost() -> None:
class CountingSet(set):
lookups = 0
def __contains__(self, value: object) -> bool:
self.lookups += 1
return super().__contains__(value)
assembler = StreamAssembler(origin=Origin("p", "openai_chat", "m"), protocol="openai_chat")
allocated = CountingSet()
assembler._allocated_tool_ids = allocated
count = 100
for index in range(count):
assembler.tool_start(index, name="read", native_id=f"call_{count + index}")
for index in range(count, count * 2):
assembler.tool_start(index, name="read")
assert len(allocated) == count * 2
assert allocated.lookups <= count * 3
def test_shared_tool_id_uniqueness_does_not_copy_owners() -> None:
class Owners(set):
def __sub__(self, _: object) -> set:
pytest.fail("tool uniqueness copied the complete owner set")
assembler = StreamAssembler(origin=Origin("p", "openai_responses", "m"), protocol="openai_responses")
assembler.tool_start(0, call_id="dup")
assembler._tool_id_owners["dup"] = Owners({0})
assembler.tool_start(1, name="read", native_id="dup")
@pytest.mark.asyncio
@pytest.mark.parametrize("indexed", [False, True])
@pytest.mark.parametrize("item_id", ["fc_1", None])
async def test_responses_late_call_id_is_stable_and_replayable(indexed: bool, item_id: str | None) -> None:
address = {"output_index": 0} if indexed else {}
identity = {"id": item_id} if item_id else {}
delta_identity = {"item_id": item_id} if item_id else {}
frames = [
{"type": "response.output_item.added", **address,
"item": {"type": "function_call", **identity, "name": "read"}},
{"type": "response.function_call_arguments.delta", **address,
**delta_identity, "delta": '{"path":"x"}'},
{"type": "response.output_item.done", **address,
"item": {"type": "function_call", **identity, "call_id": "call_1", "name": "read"}},
{"type": "response.completed", "response": {"status": "completed"}},
]
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
message = events[-1].message
call = message.tool_calls[0]
start = next(event for event in events if isinstance(event, ToolCallStart))
assert start.id == call.id == "call_1"
assert call.native_id == item_id
assert call.arguments == {"path": "x"}
replay = build_responses_payload(_request("openai_responses", messages=(message,)), (message,))
assert replay["input"][0]["call_id"] == "call_1"
def test_closed_tool_identity_does_not_retain_unused_history() -> None:
assembler = StreamAssembler(origin=Origin("p", "openai_responses", "m"), protocol="openai_responses")
assembler.tool_start(0, name="read", call_id="call", native_id="fc")
assembler.block_end(0)
for index in range(100):
assembler.tool_start(0, name="read", call_id="call" + "x" * index, native_id="fc")
assert assembler._allocated_tool_ids == {"call"}
@pytest.mark.asyncio
@pytest.mark.parametrize("conflicting", [True, False])
@pytest.mark.parametrize("case", [
"chat-start", "chat-pending", "chat-id-only", "anthropic-start",
"responses-added", "responses-done", "responses-delta", "responses-arguments-done",
])
async def test_open_tool_slot_rejects_conflicting_native_identity(case: str, conflicting: bool) -> None:
# A second identity must not borrow the first call's name/arguments. Prior
# tests cover late/missing IDs, but not an index reused by a different call.
later_id = "second" if conflicting else "first"
if case.startswith("chat"):
protocol = "openai_chat"
first = {"name": "read", "arguments": "{}"} if case != "chat-pending" else {}
second = {"name": "read", "arguments": "{}" if case == "chat-pending" else " "}
if case == "chat-id-only":
second = None
frames = [
{"choices": [{"delta": {"tool_calls": [{"index": 0, "id": "first", "function": first}]}}]},
{"choices": [{"delta": {"tool_calls": [
{"index": 0, "id": later_id, **({"function": second} if second is not None else {})},
]}, "finish_reason": "tool_calls"}]},
]
elif case.startswith("anthropic"):
protocol = "anthropic"
frames = [
{"type": "content_block_start", "index": 0,
"content_block": {"type": "tool_use", "id": identity, "name": "read", "input": {}}}
for identity in ("first", later_id)
] + [
{"type": "message_delta", "delta": {"stop_reason": "tool_use"}},
{"type": "message_stop"},
]
else:
protocol = "openai_responses"
frames = [
{"type": "response.output_item.added", "output_index": 0, "item": {
"type": "function_call", "id": "first", "call_id": "call_first",
"name": "read", "arguments": "{}",
}},
]
if case in {"responses-added", "responses-done"}:
frames.append({
"type": f"response.output_item.{case.removeprefix('responses-')}", "output_index": 0,
"item": {"type": "function_call", "id": later_id, "name": "read", "arguments": "{}"},
})
else:
# Already-known alias for another slot must not redirect index 0.
frames.append({"type": "response.output_item.added", "output_index": 1, "item": {
"type": "function_call", "id": later_id, "call_id": "call_second", "name": "read",
}})
is_delta = case == "responses-delta"
frames.append({
"type": f"response.function_call_arguments.{'delta' if is_delta else 'done'}",
"output_index": 0, "item_id": later_id, "delta" if is_delta else "arguments": " ",
})
for index in range(2 if case in {"responses-delta", "responses-arguments-done"} else 1):
frames.append({
"type": "response.output_item.done", "output_index": index,
"item": {"type": "function_call", "name": "read", "arguments": "{}"},
})
frames.append({"type": "response.completed", "response": {"status": "completed"}})
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
if protocol == "openai_chat":
body += "data: [DONE]\n\n"
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(adapter_class(protocol)(client), _request(protocol))
if not conflicting:
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].native_id == "first"
return
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "conflicting native ID" in error.message
assert error.retryable is False
assert len([event for event in events if isinstance(event, (Done, ProviderError))]) == 1
if error.partial and error.partial.tool_calls:
assert error.partial.tool_calls[0].native_id == "first"
@pytest.mark.asyncio
async def test_responses_explicit_indexes_override_shared_native_aliases() -> None:
# Duplicate provider IDs across distinct slots may be normalized; they must
# not cause indexed argument frames to merge into the alias's latest owner.
frames = [
{"type": "response.output_item.added", "output_index": index, "item": {
"type": "function_call", "id": "shared", "call_id": f"call_{index}", "name": "read",
}}
for index in range(2)
]
for index in range(2):
frames.extend([
{"type": "response.function_call_arguments.delta", "output_index": index,
"item_id": "shared", "delta": json.dumps({"path": str(index)})},
{"type": "response.output_item.done", "output_index": index,
"item": {"type": "function_call", "id": "shared"}},
])
frames.append({"type": "response.completed", "response": {"status": "completed"}})
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert [call.arguments for call in events[-1].message.tool_calls] == [{"path": "0"}, {"path": "1"}]
assert [call.id for call in events[-1].message.tool_calls] == ["call_0", "call_1"]
@pytest.mark.asyncio
@pytest.mark.parametrize("identity_at", ["added", "done"])
async def test_responses_parallel_calls_have_unique_explicit_canonical_ids(identity_at: str) -> None:
frames = []
for index in range(2):
item = {"type": "function_call", "id": f"fc_{index}", "name": "read"}
if identity_at == "added":
item["call_id"] = "call_1"
frames.append({"type": "response.output_item.added", "output_index": index, "item": item})
for index in range(2):
frames.extend([
{"type": "response.function_call_arguments.delta", "output_index": index,
"item_id": f"fc_{index}", "delta": json.dumps({"path": str(index)})},
{"type": "response.output_item.done", "output_index": index, "item": {
"type": "function_call", "id": f"fc_{index}", "call_id": "call_1", "name": "read",
}},
])
frames.append({"type": "response.completed", "response": {"status": "completed"}})
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
starts = [event for event in events if isinstance(event, ToolCallStart)]
assert len(starts) == 2
assert len({event.id for event in starts}) == 2
assert isinstance(events[-1], Done)
calls = events[-1].message.tool_calls
assert [call.id for call in calls] == [event.id for event in starts]
assert calls[0].id == "call_1"
assert [call.arguments for call in calls] == [{"path": "0"}, {"path": "1"}]
assert [call.native_id for call in calls] == ["fc_0", "fc_1"]
@pytest.mark.asyncio
async def test_anthropic_streams_thinking_tool_arguments_and_usage() -> None:
body = (
'data: {"type":"message_start","message":{"usage":{"input_tokens":5}}}\r\n\r\n'
'data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking"}}\r\n\r\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"reason"}}\r\n\r\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"signature_delta","signature":"sig"}}\r\n\r\n'
'data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_1","name":"read"}}\r\n\r\n'
'data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\\"path\\":\\"x\\"}"}}\r\n\r\n'
'data: {"type":"content_block_stop","index":0}\r\n\r\n'
'data: {"type":"content_block_stop","index":1}\r\n\r\n'
'data: {"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":3}}\r\n\r\n'
'data: {"type":"message_stop"}\r\n\r\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "tool_use"
assert final.message.usage == Usage(input_tokens=5, output_tokens=3)
assert final.message.content == (
ThinkingBlock("reason", "sig"),
ToolCallBlock("toolu_1", "read", {"path": "x"}, native_id="toolu_1"),
)
assert [event.index for event in events if isinstance(event, BlockEnd)] == [0, 1]
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body", "expected_path"),
[
(
AnthropicAdapter,
"anthropic",
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n',
"/v1/messages",
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n",
"/v1/chat/completions",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
"/v1/responses",
),
],
)
async def test_adapters_join_protocol_path_before_existing_query(
adapter_type: Any,
protocol: str,
body: str,
expected_path: str,
) -> None:
seen: list[httpx.URL] = []
def handler(request: httpx.Request) -> httpx.Response:
seen.append(request.url)
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(
adapter_type(client),
_request(protocol, base_url="https://model.test/v1?tenant=blue"),
)
assert isinstance(events[-1], Done)
assert seen[0].path == expected_path
assert seen[0].query == b"tenant=blue"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1",'
'"function":{"name":"read","arguments":"{\\"path\\":\\"x"}}]}}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"length"}]}\n\n'
"data: [DONE]\n\n"
),
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":"tool_use","id":"toolu_1","name":"read"}}\n\n'
'data: {"type":"content_block_delta","index":0,'
'"delta":{"type":"input_json_delta","partial_json":"{\\"path\\":\\"x"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"max_tokens"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
# Generation reached the context window first: Anthropic says to treat the response as truncated.
AnthropicAdapter,
"anthropic",
(
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":"tool_use","id":"toolu_1","name":"read"}}\n\n'
'data: {"type":"content_block_delta","index":0,'
'"delta":{"type":"input_json_delta","partial_json":"{\\"path\\":\\"x"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"model_context_window_exceeded"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,'
'"delta":"{\\"path\\":\\"x"}\n\n'
'data: {"type":"response.incomplete","response":{"status":"incomplete",'
'"incomplete_details":{"reason":"max_output_tokens"}}}\n\n'
),
),
],
)
async def test_non_tool_stop_preserves_truncated_tool_calls_for_settlement(
adapter_type: Any,
protocol: str,
body: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
terminal = events[-1]
assert isinstance(terminal, Done)
assert terminal.message.stop_reason == "length"
assert [call.arguments for call in terminal.message.tool_calls] == [{}]
@pytest.mark.asyncio
async def test_anthropic_empty_initial_tool_input_does_not_prefix_streamed_json() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_1","name":"read","input":{}}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"tool_use"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_anthropic_nonempty_initial_tool_input_is_preserved_without_deltas() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_1","name":"read","input":{"path":"x"}}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"tool_use"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_anthropic_replays_redacted_thinking_payload_verbatim() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"redacted_thinking","data":"opaque-redacted"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
captured: dict[str, Any] = {}
def handler(request: httpx.Request) -> httpx.Response:
captured.update(json.loads(request.content))
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
history = (
AssistantMessage(
content=(ThinkingBlock("", "opaque-redacted", redacted=True),),
origin=Origin("anthropic", "anthropic", "model-x"),
stop_reason="stop",
),
UserMessage((TextBlock(text="continue"),)),
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(
AnthropicAdapter(client),
_request("anthropic", messages=history),
)
assistant = captured["messages"][0]
assert assistant["content"] == [{"type": "redacted_thinking", "data": "opaque-redacted"}]
final = events[-1]
assert isinstance(final, Done)
assert final.message.content == (ThinkingBlock("", "opaque-redacted", redacted=True),)
@pytest.mark.asyncio
async def test_anthropic_ignores_unknown_content_block_types_like_pi() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"future_block"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == ()
@pytest.mark.asyncio
async def test_anthropic_mid_output_fallback_is_terminal_like_pi() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"before"}}\n\n'
'data: {"type":"content_block_start","index":1,"content_block":{"type":"fallback"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "unknown"
assert events[-1].retryable is False
assert events[-1].partial is not None
@pytest.mark.asyncio
async def test_anthropic_ignores_unknown_top_level_events_like_pi() -> None:
body = (
'data: {"type":"future_event","payload":{"new":"field"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
@pytest.mark.asyncio
async def test_anthropic_ignores_unknown_sse_event_before_parsing_like_pi() -> None:
body = (
'event: future_event\n'
'data: not-json\n\n'
'event: message_delta\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'event: message_stop\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
@pytest.mark.asyncio
async def test_anthropic_named_error_frame_uses_nested_error_code() -> None:
body = (
'event: error\n'
'data: {"type":"error","error":{"type":"api_error","message":"busy"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is True
@pytest.mark.asyncio
async def test_anthropic_content_block_start_preserves_initial_content() -> None:
body = (
'event: content_block_start\n'
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":"initial"}}\n\n'
'event: content_block_start\n'
'data: {"type":"content_block_start","index":1,"content_block":{"type":"thinking","thinking":"plan","signature":"sig"}}\n\n'
'event: message_delta\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'event: message_stop\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("initial"), ThinkingBlock("plan", "sig"))
@pytest.mark.asyncio
async def test_anthropic_malformed_delta_is_terminal_invalid_request() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":null}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("block", "delta"),
[
("text", {"type": "text_delta", "text": 7}),
("thinking", {"type": "thinking_delta", "thinking": []}),
("thinking", {"type": "signature_delta", "signature": {}}),
("tool", {"type": "input_json_delta", "partial_json": 7}),
],
)
async def test_anthropic_known_delta_fields_reject_wrong_types(
block: str,
delta: dict[str, Any],
) -> None:
content_block = {
"text": {"type": "text"},
"thinking": {"type": "thinking"},
"tool": {"type": "tool_use", "id": "toolu_1", "name": "read"},
}[block]
body = (
f'data: {json.dumps({"type": "content_block_start", "index": 0, "content_block": content_block})}\n\n'
f'data: {json.dumps({"type": "content_block_delta", "index": 0, "delta": delta})}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
@pytest.mark.asyncio
async def test_anthropic_pre_output_fallback_is_ignored_like_pi() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"fallback"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == ()
@pytest.mark.asyncio
async def test_anthropic_wrong_kind_delta_is_ignored_like_pi() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"ignored"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock(""),)
@pytest.mark.asyncio
async def test_anthropic_malformed_message_delta_usage_is_terminal_invalid_request() -> None:
body = (
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":[]}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
@pytest.mark.asyncio
async def test_anthropic_ignores_unknown_delta_variants_like_pi() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"future_delta","value":"ignored"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock(""),)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("stop", "details", "error_message"),
[
(
"refusal",
{"type": "refusal", "category": "reasoning_extraction", "explanation": "Claude can't help with this."},
"category: reasoning_extraction; explanation: Claude can't help with this.",
),
("refusal", {"category": "cyber", "explanation": None}, "category: cyber"),
# A diagnostic never fails the response: a shape it cannot read is left out.
("refusal", [], None),
("refusal", {"category": 7}, None),
("end_turn", {"category": "cyber"}, None),
],
)
async def test_anthropic_refusal_keeps_its_stop_details_as_the_error_message(
stop: str, details: Any, error_message: str | None
) -> None:
delta = json.dumps({"type": "message_delta", "delta": {"stop_reason": stop, "stop_details": details}})
body = f'data: {delta}\n\ndata: {{"type":"message_stop"}}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == ("refusal" if stop == "refusal" else "stop")
assert events[-1].message.error_message == error_message
@pytest.mark.asyncio
async def test_anthropic_empty_block_end_does_not_count_as_streamed_output() -> None:
body = (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_stop","index":0}\n\n'
'data: {"type":"error","error":{"type":"overloaded_error","message":"busy"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert not any(isinstance(event, BlockEnd) for event in events)
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "overloaded"
assert error.retryable is True
@pytest.mark.asyncio
async def test_responses_sets_client_side_state_rules_and_keeps_reasoning_item() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_text.delta","output_index":0,"delta":"plan"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","encrypted_content":"enc"}}\n\n'
'data: {"type":"response.output_item.added","output_index":1,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":1,"delta":"done"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":[{"type":"reasoning","id":"rs_1","encrypted_content":"enc"},{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"done"}]}],"usage":{"input_tokens":2,"output_tokens":4,"output_tokens_details":{"reasoning_tokens":1}}}}\n\n'
)
captured: dict[str, Any] = {}
def handler(request: httpx.Request) -> httpx.Response:
captured.update(json.loads(request.content))
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert captured["store"] is False
assert "previous_response_id" not in captured
assert captured["include"] == ["reasoning.encrypted_content"]
final = events[-1]
assert isinstance(final, Done)
assert final.message.content[0] == ThinkingBlock(
"plan",
json.dumps(
{"type": "reasoning", "id": "rs_1", "encrypted_content": "enc"},
separators=(",", ":"),
),
)
assert final.message.content[1] == TextBlock("done")
assert final.message.usage == Usage(input_tokens=2, output_tokens=4, reasoning_tokens=1)
@pytest.mark.asyncio
async def test_responses_summary_done_adds_separator() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_summary_part.added","output_index":0}\n\n'
'data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"plan"}\n\n'
'data: {"type":"response.reasoning_summary_part.done","output_index":0}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (ThinkingBlock("plan\n\n", None),)
@pytest.mark.asyncio
async def test_responses_summary_done_rejects_non_integer_output_index() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_summary_part.done","output_index":"0"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
@pytest.mark.asyncio
async def test_responses_known_lifecycle_events_are_accounted_for() -> None:
body = (
'data: {"type":"response.created","response":{"id":"r","status":"in_progress"}}\n\n'
'data: {"type":"response.in_progress","response":{"status":"in_progress"}}\n\n'
'data: {"type":"response.queued","response":{"status":"queued"}}\n\n'
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_summary_part.added","output_index":0}\n\n'
'data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"plan"}\n\n'
'data: {"type":"response.reasoning_summary_part.done","output_index":0}\n\n'
'data: {"type":"response.content_part.added","output_index":0}\n\n'
'data: {"type":"response.content_part.done","output_index":0}\n\n'
'data: {"type":"response.output_text.done","output_index":0}\n\n'
'data: {"type":"response.reasoning_text.done","output_index":0}\n\n'
'data: {"type":"response.reasoning_summary_text.done","output_index":0}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (ThinkingBlock("plan\n\n"),)
@pytest.mark.asyncio
async def test_responses_custom_tool_events_are_ignored_without_custom_tool_blocks() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"custom_tool_call","id":"ct_1","call_id":"call_1","name":"lookup"}}\n\n'
'data: {"type":"response.custom_tool_call_input.delta","output_index":0,"delta":"{\\"query\\":\\"x\\"}"}\n\n'
'data: {"type":"response.custom_tool_call_input.done","output_index":0,"input":"{\\"query\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"custom_tool_call","id":"ct_1","call_id":"call_1","name":"lookup","input":"{\\"query\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == ()
@pytest.mark.asyncio
async def test_responses_top_level_error_frame_is_classified() -> None:
body = 'data: {"type":"error","error":{"code":"server_error","message":"failed"}}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "server"
assert events[-1].retryable is True
@pytest.mark.asyncio
async def test_chat_accepts_integer_error_code_from_openai_compatible_servers() -> None:
body = (
'data: {"error":{"type":"InternalServerError","code":500,'
'"message":"Internal server error"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("body", "expected_kind"),
[
(
'data: {"type":"error","error":{"type":"context_length_exceeded"}}\n\n',
"overflow",
),
(
'data: {"type":"response.failed","response":{"status":"failed","error":{"type":"context_length_exceeded"}}}\n\n',
"overflow",
),
(
'data: {"type":"response.completed","response":{"status":"failed","error":{"type":"context_length_exceeded"}}}\n\n',
"overflow",
),
],
)
async def test_responses_error_classification_uses_the_complete_envelope(
body: str,
expected_kind: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == expected_kind
@pytest.mark.asyncio
async def test_responses_error_message_excludes_response_request_metadata() -> None:
body = (
'data: {"type":"response.failed","response":{"status":"failed",'
'"error":{"code":"server_error","message":"upstream failed"},'
'"instructions":"secret system prompt","tools":[{"name":"secret"}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.message == "upstream failed"
assert "secret system prompt" not in error.message
@pytest.mark.asyncio
async def test_responses_failed_without_error_details_has_safe_fixed_message() -> None:
body = (
'data: {"type":"response.failed","response":{"status":"failed","error":null,'
'"instructions":"If you hit a rate limit, wait.","tools":[{"name":"secret"}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "unknown"
assert error.retryable is False
assert error.message == "OpenAI Responses response failed"
@pytest.mark.asyncio
async def test_responses_completed_without_status_follows_pi_default() -> None:
body = 'data: {"type":"response.completed","response":{"output":[]}}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "stop"
@pytest.mark.asyncio
async def test_responses_completed_with_null_output_follows_pi_default() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"hi"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":null}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "stop"
assert final.message.content == (TextBlock("hi"),)
@pytest.mark.asyncio
async def test_responses_ignores_unread_fields_on_non_function_output_items() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"tool_search_call","arguments":{}}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,'
'"item":{"type":"tool_search_call","arguments":{}}}\n\n'
'data: {"type":"response.output_item.added","output_index":1,'
'"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":1,"delta":"hi"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":null}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.content == (TextBlock("hi"),)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body", "expected_kind"),
[
(
AnthropicAdapter,
"anthropic",
'event: error\ndata: {"type":"error","error":{"type":"overloaded_error","code":529,"message":"Overloaded"}}\n\n',
"overloaded",
),
],
)
async def test_provider_error_ignores_unconsumed_code_types(
adapter_type: Any,
protocol: str,
body: str,
expected_kind: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == expected_kind
assert error.retryable is True
@pytest.mark.asyncio
@pytest.mark.parametrize(
("reason", "expected"),
[("content_filter", "safety"), ("unknown_reason", "error")],
)
async def test_responses_incomplete_reasons_have_explicit_outcomes(reason: str, expected: str) -> None:
body = (
f'data: {{"type":"response.incomplete","response":{{"status":"incomplete","incomplete_details":{{"reason":"{reason}"}}}}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
terminal = events[-1]
if expected == "safety":
assert isinstance(terminal, Done)
assert terminal.message.stop_reason == "safety"
else:
assert isinstance(terminal, ProviderError)
assert terminal.kind == "unknown"
@pytest.mark.asyncio
async def test_responses_incomplete_without_status_is_not_success() -> None:
body = (
'data: {"type":"response.incomplete","response":'
'{"incomplete_details":{"reason":"unknown_reason"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
terminal = events[-1]
assert isinstance(terminal, ProviderError)
assert terminal.kind == "unknown"
assert terminal.partial is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
(
'event: message_start\n'
'data: {"type":"message_start","message":{"usage":{"input_tokens":1}}}\n\n'
'event: content_block_start\n'
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":"thinking","thinking":""}}\n\n'
'event: content_block_delta\n'
'data: {"type":"content_block_delta","index":0,'
'"delta":{"type":"thinking_delta","thinking":"ab"}}\n\n'
'event: content_block_delta\n'
'data: {"type":"content_block_delta","index":0,'
'"delta":{"type":"thinking_delta","thinking":"cd"}}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"content":"ab"}}]}\n\n'
'data: {"choices":[{"delta":{"content":"cd"}}]}\n\n'
),
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta",'
'"output_index":0,"delta":"ab"}\n\n'
'data: {"type":"response.function_call_arguments.delta",'
'"output_index":0,"delta":"cd"}\n\n'
),
),
],
)
async def test_cumulative_output_budget_covers_text_thinking_and_tool_arguments(
monkeypatch: pytest.MonkeyPatch,
adapter_type: Any,
protocol: str,
body: str,
) -> None:
# Fixed wire data, independent of the code's computed accounting. The cap
# admits identity/slot overhead and the first delta, but not the second.
body = body.replace('"ab"', '"' + "a" * 8192 + '"')
body = body.replace('"cd"', '"' + "b" * 8192 + '"')
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 16384)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.retryable is False
assert "output budget" in error.message.lower()
assert error.partial is not None
delta_type = {
AnthropicAdapter: ThinkingDelta,
OpenAIChatAdapter: TextDelta,
OpenAIResponsesAdapter: ToolCallDelta,
}[adapter_type]
deltas = [event for event in events if isinstance(event, delta_type)]
assert len(deltas) == 1
if adapter_type is OpenAIResponsesAdapter:
assert deltas[0].arguments_delta == "a" * 8192
else:
assert deltas[0].delta == "a" * 8192
@pytest.mark.asyncio
async def test_cumulative_output_budget_covers_opaque_reasoning_details(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 4096)
opaque = "x" * 8192
body = (
"data: "
+ json.dumps(
{
"choices": [
{
"delta": {
"reasoning_details": [
{"type": "reasoning.encrypted", "data": opaque}
]
},
"finish_reason": "stop",
}
]
}
)
+ "\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "output budget" in error.message.lower()
def test_cumulative_output_budget_covers_tool_identity(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 8192)
assembler = StreamAssembler(
origin=Origin("openai", "openai_chat", "model"),
protocol="openai_chat",
)
with pytest.raises(OutputBudgetExceeded):
assembler.tool_start(
"tool",
name="n" * 16384,
call_id="call",
native_id="native",
)
def test_cumulative_output_budget_covers_raw_responses_reasoning_items(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 8192)
assembler = StreamAssembler(
origin=Origin("openai", "openai_responses", "model"),
protocol="openai_responses",
)
with pytest.raises(OutputBudgetExceeded):
assembler.merge_reasoning_item(
0,
{
"type": "reasoning",
"id": "rs_1",
"summary": [],
"content": [{"type": "summary_text", "text": "x" * 16384}],
},
)
def test_cumulative_output_budget_covers_unstarted_tool_aliases(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 4096)
assembler = StreamAssembler(
origin=Origin("openai", "openai_chat", "model"),
protocol="openai_chat",
)
with pytest.raises(OutputBudgetExceeded):
for index in range(4):
assembler.bind_tool_alias(
("tool", index),
f"call-{index}-" + ("x" * 2048),
)
def test_cumulative_output_budget_covers_empty_tool_states(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 8192)
assembler = StreamAssembler(
origin=Origin("openai", "openai_chat", "model"),
protocol="openai_chat",
)
with pytest.raises(OutputBudgetExceeded):
for index in range(5):
assembler.tool_start(("tool", index))
def test_cumulative_output_budget_charges_empty_content_slots(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(common_module, "MAX_CUMULATIVE_OUTPUT_CHARS", 4096)
assembler = StreamAssembler(
origin=Origin("anthropic", "anthropic", "model"),
protocol="anthropic",
)
assembler.ensure_text_slot(0)
assembler.ensure_text_slot(1)
assert assembler._output_chars == 4096
with pytest.raises(OutputBudgetExceeded):
assembler.ensure_text_slot(2)
@pytest.mark.asyncio
async def test_many_small_chat_deltas_preserve_the_complete_text() -> None:
count = 20_000
body = (
'data: {"choices":[{"delta":{"content":"x"}}]}\n\n' * count
+ 'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
+ "data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.content == (TextBlock(text="x" * count),)
@pytest.mark.asyncio
@pytest.mark.parametrize("status", ["failed", "cancelled"])
async def test_responses_failed_or_cancelled_status_is_terminal_provider_error(status: str) -> None:
body = f'data: {{"type":"response.completed","response":{{"status":"{status}"}}}}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "unknown"
@pytest.mark.asyncio
async def test_responses_non_array_terminal_output_is_malformed() -> None:
body = (
'data: {"type":"response.completed","response":{"status":"completed","output":{}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "output" in error.message
@pytest.mark.asyncio
async def test_responses_terminal_output_item_type_must_be_a_string() -> None:
body = (
'data: {"type":"response.completed","response":{"status":"completed",'
'"output":[{"type":7}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "type" in error.message
def test_responses_replay_preserves_output_item_order() -> None:
history = (
AssistantMessage(
content=(
ThinkingBlock("plan", json.dumps({"id": "rs_1", "encrypted_content": "enc"}, separators=(",", ":"))),
TextBlock(text="answer"),
),
origin=Origin("openai", "openai_responses", "model-x"),
stop_reason="stop",
),
)
payload = build_responses_payload(
_request("openai_responses", messages=history),
history,
)
assert payload["input"][0]["type"] == "reasoning"
assert payload["input"][1]["role"] == "assistant"
@pytest.mark.asyncio
async def test_responses_uses_item_id_for_parallel_argument_deltas_and_incomplete_stop() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_item_a","call_id":"call_a","name":"read"}}\n\n'
'data: {"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_item_b","call_id":"call_b","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","item_id":"fc_item_b","delta":"{\\"path\\":\\"b\\"}"}\n\n'
'data: {"type":"response.function_call_arguments.delta","item_id":"fc_item_a","delta":"{\\"path\\":\\"a\\"}"}\n\n'
'data: {"type":"response.incomplete","response":{"status":"incomplete","incomplete_details":{"reason":"max_output_tokens"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "length"
assert [call.arguments for call in final.message.tool_calls] == [{"path": "a"}, {"path": "b"}]
@pytest.mark.asyncio
async def test_responses_argument_done_emits_only_the_unseen_suffix() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\\"path\\":"}\n\n'
'data: {"type":"response.function_call_arguments.done","output_index":0,"arguments":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.arguments_delta for event in events if isinstance(event, ToolCallDelta)] == [
'{"path":',
'"x"}',
]
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_empty_output_item_arguments_keep_streamed_arguments() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":""}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_reasoning_signature_survives_terminal_snapshot_without_encrypted_content() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","encrypted_content":"enc1"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":[{"type":"reasoning","id":"rs_1","summary":[]}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert json.loads(final.message.content[0].signature or "")["encrypted_content"] == "enc1"
@pytest.mark.asyncio
async def test_responses_late_tool_argument_delta_after_close_is_ignored() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"LATE"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_rejects_unfinished_tool_call_on_completed_response() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "invalid_request"
assert "unfinished tool call" in events[-1].message
@pytest.mark.asyncio
async def test_responses_ignores_final_only_function_call_like_pi() -> None:
body = (
'data: {"type":"response.completed","response":{"status":"completed","output":[{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{}"}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "stop"
assert events[-1].message.tool_calls == ()
@pytest.mark.asyncio
async def test_responses_done_alias_follows_terminal_response_state() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.done","response":{"status":"completed","output":[{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"done"}]}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "stop"
@pytest.mark.asyncio
async def test_responses_does_not_adopt_terminal_message_snapshot() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":[{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"hello "},{"type":"output_text","text":"world"}]}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock(""),)
@pytest.mark.asyncio
async def test_responses_refusal_is_not_normalized_to_stop() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.refusal.delta","delta":"cannot help"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "refusal"
@pytest.mark.asyncio
async def test_responses_orphan_refusal_delta_is_ignored_like_pi() -> None:
body = (
'data: {"type":"response.refusal.delta","output_index":3,"delta":"cannot help"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "stop"
assert final.message.content == ()
@pytest.mark.asyncio
async def test_responses_refusal_does_not_skip_message_snapshot_merge() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.refusal.delta","output_index":0,"delta":"cannot"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"cannot help"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.delta for event in events if isinstance(event, TextDelta)] == [
"cannot",
" help",
]
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("cannot help"),)
@pytest.mark.asyncio
async def test_responses_terminal_refusal_output_sets_refusal_stop_reason() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed","output":[{"type":"message","id":"msg_1","content":[{"type":"refusal","refusal":"cannot help"}]}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "refusal"
@pytest.mark.asyncio
async def test_responses_failed_server_code_is_retryable_before_streaming() -> None:
body = (
'data: {"type":"response.failed","response":{"status":"failed","error":{"code":"server_error","message":"upstream failed"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is True
@pytest.mark.asyncio
async def test_responses_reasoning_only_failure_has_no_partial() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"reasoning","id":"rs_1","summary":[],"content":[]}}\n\n'
'data: {"type":"response.failed","response":{"status":"failed",'
'"error":{"code":"server_error","message":"upstream failed"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.partial is None
@pytest.mark.asyncio
async def test_responses_failed_tool_start_is_not_replayed_in_partial() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.failed","response":{"status":"failed","error":{"code":"server_error","message":"upstream failed"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.retryable is False
assert error.partial is not None
assert error.partial.tool_calls == ()
@pytest.mark.asyncio
async def test_responses_orphan_argument_events_are_ignored_like_pi() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","delta":"orphan"}\n\n'
'data: {"type":"response.function_call_arguments.done","arguments":"{\\"orphan\\":true}"}\n\n'
'data: {"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {}
@pytest.mark.asyncio
async def test_responses_output_item_done_can_create_a_function_call_slot() -> None:
body = (
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert any(isinstance(event, ToolCallStart) for event in events)
assert [event.arguments_delta for event in events if isinstance(event, ToolCallDelta)] == [
'{"path":"x"}'
]
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_missing_output_index_uses_one_shared_fallback_slot() -> None:
body = (
'data: {"type":"response.output_item.added","item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","delta":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.arguments_delta for event in events if isinstance(event, ToolCallDelta)] == [
'{"path":"x"}'
]
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_consecutive_missing_output_indexes_rebind_tool_slots() -> None:
body = (
'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"fc_a","call_id":"call_a","name":"read"}}\n\n'
'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"fc_b","call_id":"call_b","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","item_id":"fc_a","delta":"{\\"path\\":\\"a\\"}"}\n\n'
'data: {"type":"response.function_call_arguments.delta","item_id":"fc_b","delta":"{\\"path\\":\\"b\\"}"}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"function_call","id":"fc_a","call_id":"call_a","name":"read","arguments":"{\\"path\\":\\"a\\"}"}}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"function_call","id":"fc_b","call_id":"call_b","name":"read","arguments":"{\\"path\\":\\"b\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert [call.arguments for call in final.message.tool_calls] == [
{"path": "a"},
{"path": "b"},
]
@pytest.mark.asyncio
async def test_responses_missing_tool_ids_reuse_the_open_fallback_slot() -> None:
body = (
'data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","delta":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
starts = [event for event in events if isinstance(event, ToolCallStart)]
final = events[-1]
assert len(starts) == 1
assert isinstance(final, Done)
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_non_tool_missing_output_index_keeps_pi_fallback() -> None:
body = (
'data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","delta":"{\\"path\\":\\"x\\"}"}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"function_call","call_id":"call_1","name":"read","arguments":"{\\"path\\":\\"x\\"}"}}\n\n'
'data: {"type":"response.output_item.added","item":{"type":"message"}}\n\n'
'data: {"type":"response.output_text.delta","delta":"hello"}\n\n'
'data: {"type":"response.output_item.done","item":{"type":"message","content":[{"type":"output_text","text":"hello"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.content == (
ToolCallBlock("call_1", "read", {"path": "x"}),
TextBlock("hello"),
)
@pytest.mark.asyncio
async def test_responses_empty_slots_close_before_late_deltas() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_item.added","output_index":1,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[]}}\n\n'
'data: {"type":"response.output_item.done","output_index":1,"item":{"type":"reasoning","id":"rs_1","summary":[]}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"late text"}\n\n'
'data: {"type":"response.reasoning_text.delta","output_index":1,"delta":"late thinking"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert not [event for event in events if isinstance(event, TextDelta)]
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock(""), ThinkingBlock(""))
@pytest.mark.asyncio
async def test_responses_output_item_done_replaces_a_different_text_snapshot() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"Hello world"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"Hello, world"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("Hello, world"),)
@pytest.mark.asyncio
async def test_responses_output_item_done_closes_slot_before_late_delta() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"a"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"a"}]}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"LATE"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.delta for event in events if isinstance(event, TextDelta)] == ["a"]
assert [event.index for event in events if isinstance(event, BlockEnd)] == [0]
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("a"),)
@pytest.mark.asyncio
async def test_chat_completions_reassembles_parallel_calls() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_a","function":{"name":"read","arguments":"{\\"path\\":\\"a\\"}"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"index":1,"id":"call_b","function":{"name":"read","arguments":"{\\"path\\":\\"b\\"}"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "tool_use"
assert [call.arguments for call in final.message.tool_calls] == [{"path": "a"}, {"path": "b"}]
@pytest.mark.asyncio
@pytest.mark.parametrize(
"tool_deltas",
[
pytest.param(
[{"index": 0, "id": "call_a", "function": {"name": "read", "arguments": "{}"},
"extra_content": {"google": {"thought_signature": "sig-a"}}}],
id="with-call",
),
pytest.param(
[{"index": 0, "id": "call_a", "function": {"name": "read", "arguments": "{}"}},
{"index": 0, "extra_content": {"google": {"thought_signature": "sig-a"}}}],
id="own-delta",
),
],
)
async def test_chat_replays_gemini_tool_call_thought_signature(tool_deltas: list[dict[str, Any]]) -> None:
"""Gemini answers the request after a tool call with 400 unless that call's thought signature comes back."""
frames = [{"choices": [{"delta": {"tool_calls": [delta]}}]} for delta in tool_deltas]
frames.append({"choices": [{"delta": {}, "finish_reason": "tool_calls"}]})
served = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames) + "data: [DONE]\n\n"
answered = 'data: {"choices":[{"delta":{"content":"done"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n'
bodies: list[dict[str, Any]] = []
def handler(request: httpx.Request) -> httpx.Response:
bodies.append(json.loads(request.content))
text = served if len(bodies) == 1 else answered
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=text)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
adapter = OpenAIChatAdapter(client)
first = await _events(adapter, _request("openai_chat"))
assert isinstance(first[-1], Done)
history = (
UserMessage((TextBlock(text="hello"),)),
first[-1].message,
ToolResultMessage("call_a", "read", (TextBlock(text="ok"),)),
)
second = await _events(adapter, _request("openai_chat", messages=history))
assert isinstance(second[-1], Done)
assistant = next(message for message in bodies[1]["messages"] if message["role"] == "assistant")
assert assistant["tool_calls"] == [{
"id": "call_a",
"type": "function",
"function": {"name": "read", "arguments": "{}"},
"extra_content": {"google": {"thought_signature": "sig-a"}},
}]
@pytest.mark.asyncio
async def test_chat_reuses_indexless_tool_slot_for_argument_only_delta() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"id":"call_a","function":{"name":"read"}}]}}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"function":{"arguments":"{\\"path\\":\\"x\\"}"}}]},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.tool_calls[0].id == "call_a"
assert final.message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("first", "second"),
[
(
{"index": 1, "id": "call_a", "function": {"name": "read"}},
{"id": "call_b", "function": {"name": "write", "arguments": '{"q":2}'}},
),
(
{"id": "call_a", "function": {"name": "read"}},
{"index": 0, "id": "call_b", "function": {"name": "write", "arguments": '{"q":2}'}},
),
(
{"id": "call_a", "function": {"name": "read"}},
{"id": "call_b", "function": {"name": "write", "arguments": '{"q":2}'}},
),
],
)
async def test_chat_indexed_and_indexless_tool_calls_do_not_collide(
first: dict[str, Any],
second: dict[str, Any],
) -> None:
body = (
f'data: {json.dumps({"choices": [{"delta": {"tool_calls": [first]}}]})}\n\n'
f'data: {json.dumps({"choices": [{"delta": {"tool_calls": [second]}}]})}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
starts = [event for event in events if isinstance(event, ToolCallStart)]
final = events[-1]
assert isinstance(final, Done)
assert [start.name for start in starts] == ["read", "write"]
assert [call.name for call in final.message.tool_calls] == ["read", "write"]
assert [call.arguments for call in final.message.tool_calls] == [{}, {"q": 2}]
@pytest.mark.asyncio
async def test_chat_uses_choice_usage_fallback() -> None:
body = (
'data: {"choices":[{"delta":{"content":"ok"},"usage":{"prompt_tokens":3,"completion_tokens":2},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.usage == Usage(input_tokens=3, output_tokens=2)
@pytest.mark.asyncio
async def test_chat_reuses_tool_id_received_before_function_name() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"provider-call"}]}}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"read","arguments":"{\\"path\\":\\"x\\"}"}}]},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].id == "provider-call"
@pytest.mark.asyncio
async def test_chat_late_tool_id_matches_tool_call_start_id() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"read"}}]}}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"provider-call","function":{"arguments":"{\\"path\\":\\"x\\"}"}}]},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
start = next(event for event in events if isinstance(event, ToolCallStart))
final = events[-1]
assert isinstance(final, Done)
assert start.id == final.message.tool_calls[0].id
assert final.message.tool_calls[0].native_id == "provider-call"
@pytest.mark.asyncio
async def test_chat_malformed_tool_arguments_keep_text_and_usage_in_partial() -> None:
body = (
'data: {"choices":[{"delta":{"content":"before "}}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_good","function":{"name":"read","arguments":"{\\"path\\":\\"x\\"}"}},{"index":1,"id":"call_bad","function":{"name":"read","arguments":"not-json"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.partial is not None
assert error.partial.usage == Usage(input_tokens=3, output_tokens=2)
assert error.partial.content == (
TextBlock("before "),
ToolCallBlock(id="call_good", name="read", arguments={"path": "x"}, native_id="call_good"),
ToolCallBlock(id="call_bad", name="read", arguments={}, native_id="call_bad"),
)
assert error.partial.error_message == "malformed tool arguments: expected a JSON object"
assert error.continuable is True
assert error.malformed_tool_calls == (MalformedToolCall(id="call_bad", name="read"),)
@pytest.mark.asyncio
async def test_chat_accumulates_reasoning_details_across_deltas() -> None:
body = (
'data: {"choices":[{"delta":{"reasoning_details":[{"type":"reasoning.text","text":"a"}]}}]}\n\n'
'data: {"choices":[{"delta":{"reasoning_details":[{"type":"reasoning.text","text":"b"}]},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (
ThinkingBlock(
"",
json.dumps([{"type": "reasoning.text", "text": "ab"}], separators=(",", ":")),
),
)
@pytest.mark.asyncio
async def test_chat_finish_reason_end_maps_to_stop() -> None:
body = 'data: {"choices":[{"delta":{"content":"ok"},"finish_reason":"end"}]}\n\ndata: [DONE]\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "stop"
@pytest.mark.asyncio
@pytest.mark.parametrize(
("finish_reason", "expected"),
[("model_context_window_exceeded", "length"), ("stop", "stop"), ("content_filter", "safety")],
)
async def test_chat_context_window_finish_reason_maps_only_the_named_reason(finish_reason: str, expected: str) -> None:
body = f'data: {{"choices":[{{"delta":{{"content":"ok"}},"finish_reason":"{finish_reason}"}}]}}\n\ndata: [DONE]\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == expected
@pytest.mark.asyncio
async def test_anthropic_abort_after_message_start_keeps_usage_in_partial() -> None:
stream = _StalledStream(
b'data: {"type":"message_start","message":{"usage":{"input_tokens":7,"output_tokens":9,'
b'"output_tokens_details":{"thinking_tokens":6}}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=stream)
cancel = CancelToken()
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(AnthropicAdapter(client), _request("anthropic"), cancel))
await stream.started.wait()
await asyncio.sleep(0.05)
cancel.cancel("cancelled after usage")
events = await asyncio.wait_for(task, timeout=1)
assert isinstance(events[-1], ProviderError)
assert events[-1].partial is not None
assert events[-1].partial.content == ()
assert events[-1].partial.usage == Usage(input_tokens=7, output_tokens=9, reasoning_tokens=6)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("delta_usage", "expected"),
[
(None, Usage(7, 9, 3, 5, 6)),
({}, Usage(7, 9, 3, 5, 6)),
({"input_tokens": None, "output_tokens": None, "cache_read_input_tokens": None,
"cache_creation_input_tokens": None, "output_tokens_details": None}, Usage(7, 9, 3, 5, 6)),
({"output_tokens_details": {}}, Usage(7, 9, 3, 5, 6)),
({"output_tokens_details": {"thinking_tokens": None}}, Usage(7, 9, 3, 5, 6)),
({"cache_creation": {}}, Usage(7, 9, 3, 5, 6)),
({"cache_creation": None}, Usage(7, 9, 3, 5, 6)),
({"cache_creation_input_tokens": None, "cache_creation": {
"ephemeral_5m_input_tokens": None, "ephemeral_1h_input_tokens": None}}, Usage(7, 9, 3, 5, 6)),
({"cache_creation": {"ephemeral_5m_input_tokens": None}}, Usage(7, 9, 3, 5, 6)),
({"cache_creation": {"ephemeral_1h_input_tokens": None}}, Usage(7, 9, 3, 5, 6)),
({"cache_creation": {"ephemeral_5m_input_tokens": 2}}, Usage(7, 9, 3, 2, 6)),
({"cache_creation": {"ephemeral_1h_input_tokens": 3}}, Usage(7, 9, 3, 3, 6)),
({"cache_creation": {"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 0}}, Usage(7, 9, 3, 0, 6)),
({"cache_creation_input_tokens": 0, "cache_creation": {
"ephemeral_5m_input_tokens": 2, "ephemeral_1h_input_tokens": 3}}, Usage(7, 9, 3, 0, 6)),
({"input_tokens": 0, "output_tokens": 0, "cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0, "output_tokens_details": {"thinking_tokens": 0}},
Usage(0, 0, 0, 0, 0)),
({"input_tokens": 11, "output_tokens": 18, "cache_read_input_tokens": 4,
"cache_creation_input_tokens": 8, "output_tokens_details": {"thinking_tokens": 12}},
Usage(11, 18, 4, 8, 12)),
],
)
async def test_anthropic_usage_fields_share_start_and_delta_normalization(
delta_usage: dict[str, Any] | None, expected: Usage,
) -> None:
# Pi's non-null delta merge applies to every field. C-2 additionally keeps
# an admitted thinking-token count when it is supplied at message_start.
frames = [
{"type": "message_start", "message": {"usage": {
"input_tokens": 7, "output_tokens": 9, "cache_read_input_tokens": 3,
"cache_creation_input_tokens": 5, "output_tokens_details": {"thinking_tokens": 6},
}}},
{"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": delta_usage},
{"type": "message_stop"},
]
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.usage == expected
@pytest.mark.asyncio
@pytest.mark.parametrize(("protocol", "usage", "expected"), [
("anthropic", {
"input_tokens": 7, "output_tokens": 9, "cache_read_input_tokens": 3,
"cache_creation_input_tokens": 5,
"cache_creation": {"ephemeral_5m_input_tokens": 2, "ephemeral_1h_input_tokens": 3},
"output_tokens_details": {"thinking_tokens": 6},
"server_tool_use": {"web_fetch_requests": 1, "web_search_requests": 2},
"inference_geo": "us", "service_tier": "standard",
}, Usage(7, 9, 3, 5, 6)),
("openai_chat", {
"prompt_tokens": 100, "completion_tokens": 33, "total_tokens": 133,
"prompt_tokens_details": {
"audio_tokens": 1, "image_tokens": 2, "text_tokens": 97,
"cached_tokens": 50, "cache_write_tokens": 30,
},
"completion_tokens_details": {
"accepted_prediction_tokens": 3, "rejected_prediction_tokens": 4,
"audio_tokens": 1, "text_tokens": 11, "reasoning_tokens": 21,
},
}, Usage(20, 33, 50, 30, 21)),
("openai_responses", {
"input_tokens": 10, "input_tokens_details": {"cached_tokens": 3, "cache_write_tokens": 2},
"output_tokens": 4, "output_tokens_details": {"reasoning_tokens": 2}, "total_tokens": 14,
}, Usage(5, 4, 3, 2, 2)),
])
async def test_documented_usage_objects_map_totals_without_double_counting_breakdowns(
protocol: str, usage: dict[str, Any], expected: Usage,
) -> None:
# Official Usage/CompletionUsage/ResponseUsage fields, including ignored
# subcategories: they must not be added on top of inclusive token totals.
if protocol == "anthropic":
frames = [
{"type": "message_start", "message": {"usage": usage}},
{"type": "message_delta", "delta": {"stop_reason": "end_turn"}},
{"type": "message_stop"},
]
elif protocol == "openai_chat":
frames = [{"choices": [{"delta": {}, "finish_reason": "stop"}], "usage": usage}]
else:
frames = [{"type": "response.completed", "response": {"status": "completed", "usage": usage}}]
body = "".join(f"data: {json.dumps(frame)}\n\n" for frame in frames)
if protocol == "openai_chat":
body += "data: [DONE]\n\n"
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(adapter_class(protocol)(client), _request(protocol))
assert isinstance(events[-1], Done)
assert events[-1].message.usage == expected
def test_chat_reasoning_models_use_completion_token_limit() -> None:
payload = build_chat_payload(_request("openai_chat", model_id="gpt-5-mini"), ())
assert payload["max_completion_tokens"] == 128
assert "max_tokens" not in payload
@pytest.mark.parametrize(
("builder", "protocol", "field"),
[
(build_chat_payload, "openai_chat", "reasoning_effort"),
(build_responses_payload, "openai_responses", "reasoning"),
],
)
def test_openai_payloads_forward_explicit_none_reasoning_effort(
builder: Any,
protocol: str,
field: str,
) -> None:
payload = builder(_request(protocol, reasoning_effort="none"), ())
if field == "reasoning_effort":
assert payload[field] == "none"
else:
assert payload[field] == {"effort": "none", "summary": "auto"}
assert "include" not in payload
@pytest.mark.parametrize(
("model_id", "effort", "sent"),
[
# Native on Opus 4.7 and later (Anthropic's effort docs): the hop declares it, so it goes as given.
("claude-opus-5", "xhigh", "xhigh"),
("claude-opus-4-7", "xhigh", "xhigh"),
("claude-haiku-5-5", "xhigh", "xhigh"),
("claude-opus-4-6", "max", "max"),
# Anthropic has no minimal level.
("claude-opus-5", "minimal", "low"),
],
)
def test_anthropic_sends_the_declared_effort_as_given(model_id: str, effort: str, sent: str) -> None:
payload = build_messages_payload(_request("anthropic", model_id=model_id, reasoning_effort=effort), ())
assert payload["thinking"] == {"type": "adaptive"}
assert payload["output_config"] == {"effort": sent}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("protocol", "model_id"),
[
("anthropic", "claude-opus-5-5"),
("anthropic", "claude-opus-4-5"),
("openai_responses", "gpt-6-sol"),
("openai_chat", "deepseek-v4"),
],
)
@pytest.mark.parametrize("effort", ["minimal", "low", "high", "xhigh", "max", "none", "off", "XHIGH"])
async def test_sent_effort_is_the_token_each_adapter_puts_on_the_wire(protocol: str, model_id: str, effort: str) -> None:
"""Model Hub's effort menu reads ``sent_effort``; it must name exactly the token the request carries."""
wire = await build_wire_request(_request(protocol, model_id=model_id, reasoning_effort=effort), gateway=True)
carried = {
"anthropic": (wire.body.get("output_config") or {}).get("effort"),
"openai_responses": (wire.body.get("reasoning") or {}).get("effort"),
"openai_chat": wire.body.get("reasoning_effort"),
}[protocol]
assert carried == sent_effort(protocol, model_id, effort)
@pytest.mark.asyncio
@pytest.mark.parametrize("supports_images", [True, False])
@pytest.mark.parametrize("followed_by_user", [True, False])
async def test_chat_tool_result_images_follow_the_complete_tool_group(
supports_images: bool, followed_by_user: bool,
) -> None:
# Pi batches images after all consecutive tool results. The real adapter
# also has to load snapshots and preserve C-1's non-vision placeholders.
assistant = AssistantMessage(
content=tuple(ToolCallBlock(f"call_{i}", "read", {}) for i in range(3)),
origin=Origin("openai", "openai_chat", "model-x"),
stop_reason="tool_use",
)
messages = (
assistant,
ToolResultMessage("call_0", "read", (ImageBlock("image/png", "media-1", "one.png"),)),
ToolResultMessage("call_1", "read", (
TextBlock("Read image file"), ImageBlock("image/png", "media-2", "two.png"),
)),
ToolResultMessage("call_2", "read", ()),
)
if followed_by_user:
messages += (UserMessage((TextBlock("Compare them"),)),)
class ToolImageLoader(_Loader):
async def load(self, media_token: str) -> tuple[bytes, str]:
self.tokens.append(media_token)
return {
"media-1": (b"first", "image/jpeg"),
"media-2": (b"second", "image/png"),
}[media_token]
loader = ToolImageLoader()
captured: dict[str, Any] = {}
def handler(request: httpx.Request) -> httpx.Response:
captured.update(json.loads(request.content))
return httpx.Response(
200, text='data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n',
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(
OpenAIChatAdapter(client, media_loader=loader),
_request("openai_chat", messages=messages, supports_images=supports_images),
)
assert isinstance(events[-1], Done)
wire = captured["messages"]
assert [message["role"] for message in wire] == (
["system", "assistant", "tool", "tool", "tool"]
+ (["user"] if supports_images else [])
+ (["user"] if followed_by_user else [])
)
assert [message["tool_call_id"] for message in wire[2:5]] == ["call_0", "call_1", "call_2"]
assert [call["id"] for call in wire[1]["tool_calls"]] == ["call_0", "call_1", "call_2"]
assert wire[4]["content"] == "(no tool output)"
assert "media-1" not in json.dumps(wire)
assert "media-2" not in json.dumps(wire)
if supports_images:
assert loader.tokens == ["media-1", "media-2"]
assert wire[2]["content"] == "(see attached image)"
assert wire[3]["content"] == "Read image file"
assert wire[5]["content"] == [
{"type": "text", "text": "Attached image(s) from tool result:"},
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,Zmlyc3Q="}},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,c2Vjb25k"}},
]
else:
assert loader.tokens == []
assert wire[2]["content"] == "[image: one.png]"
assert wire[3]["content"] == "Read image file\n[image: two.png]"
if followed_by_user:
assert wire[-1] == {"role": "user", "content": "Compare them"}
@pytest.mark.asyncio
async def test_chat_emits_block_end_for_each_final_block() -> None:
body = (
'data: {"choices":[{"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert any(isinstance(event, BlockEnd) and event.index == 0 for event in events)
assert isinstance(events[-1], Done)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body", "expected"),
[
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":4,"prompt_tokens_details":{"cached_tokens":3,"cache_write_tokens":2}}}\n\n'
"data: [DONE]\n\n",
Usage(input_tokens=5, output_tokens=4, cache_read_tokens=3, cache_write_tokens=2),
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.completed","response":{"status":"completed","usage":{"input_tokens":10,"output_tokens":4,"input_tokens_details":{"cached_tokens":3,"cache_write_tokens":2},"output_tokens_details":{"reasoning_tokens":2}}}}\n\n',
Usage(input_tokens=5, output_tokens=4, cache_read_tokens=3, cache_write_tokens=2, reasoning_tokens=2),
),
],
)
async def test_provider_usage_excludes_cached_input_tokens(
adapter_type: Any,
protocol: str,
body: str,
expected: Usage,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
final = events[-1]
assert isinstance(final, Done)
assert final.message.usage == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("usage_location", ["chunk", "choice"])
@pytest.mark.parametrize(
("fields", "expected"),
[
# The first two wire fixtures come from Pi's openai-completions-tool-choice
# usage tests; the rest exercise parseChunkUsage's exact nullish order.
({"prompt_tokens": 100, "completion_tokens": 5,
"prompt_tokens_details": {"cached_tokens": 50, "cache_write_tokens": 30},
"completion_tokens_details": {"reasoning_tokens": 0}}, Usage(20, 5, 50, 30, 0)),
({"prompt_tokens": 10, "completion_tokens": 33,
"prompt_tokens_details": {"cached_tokens": 0},
"completion_tokens_details": {"reasoning_tokens": 21}}, Usage(10, 33, 0, 0, 21)),
({"prompt_tokens": 100, "prompt_tokens_details": {}, "prompt_cache_hit_tokens": 80}, Usage(20, 0, 80)),
({"prompt_tokens": 100, "prompt_tokens_details": {"cache_write_tokens": 5},
"cached_tokens": 80}, Usage(15, 0, 80, 5)),
({"prompt_tokens": 100, "prompt_tokens_details": {"cached_tokens": None},
"prompt_cache_hit_tokens": 80, "cached_tokens": 90}, Usage(20, 0, 80)),
({"prompt_tokens": 100, "prompt_tokens_details": {"cached_tokens": 0},
"prompt_cache_hit_tokens": 80, "cached_tokens": 90}, Usage(100)),
({"prompt_tokens": 100, "prompt_cache_hit_tokens": 0, "cached_tokens": 90}, Usage(100)),
({"prompt_tokens": 100, "prompt_cache_hit_tokens": None, "cached_tokens": 90}, Usage(10, 0, 90)),
({"prompt_tokens": 100, "prompt_tokens_details": None, "cached_tokens": 90}, Usage(10, 0, 90)),
({"prompt_tokens": 100, "prompt_tokens_details": {"cached_tokens": 80, "cache_write_tokens": 30}},
Usage(0, 0, 80, 30)),
],
)
async def test_chat_usage_fields_follow_pi_nullish_precedence(
usage_location: str, fields: dict[str, Any], expected: Usage,
) -> None:
choice = {"delta": {}, "finish_reason": "stop"}
frame: dict[str, Any] = {"choices": [choice]}
(frame if usage_location == "chunk" else choice)["usage"] = fields
body = f"data: {json.dumps(frame)}\n\ndata: [DONE]\n\n"
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200, text=body))) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.usage == expected
@pytest.mark.asyncio
async def test_anthropic_usage_keeps_total_cache_write_tokens() -> None:
body = (
'data: {"type":"message_start","message":{"usage":{"input_tokens":1,"cache_creation_input_tokens":25,"cache_creation":{"ephemeral_5m_input_tokens":15,"ephemeral_1h_input_tokens":10}}}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.usage == Usage(input_tokens=1, cache_write_tokens=25)
@pytest.mark.asyncio
async def test_anthropic_usage_falls_back_to_nested_cache_write_tokens() -> None:
body = (
'data: {"type":"message_start","message":{"usage":{"input_tokens":1,"cache_creation":{"ephemeral_5m_input_tokens":15,"ephemeral_1h_input_tokens":10}}}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.usage == Usage(input_tokens=1, cache_write_tokens=25)
@pytest.mark.asyncio
async def test_chat_reasoning_field_is_replayed_as_a_marker() -> None:
body = (
'data: {"choices":[{"delta":{"reasoning_content":"plan"},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.content == (ThinkingBlock("plan", "reasoning_content"),)
@pytest.mark.asyncio
async def test_malformed_tool_arguments_are_terminal_invalid_requests() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_bad","function":{"name":"read","arguments":"not-json"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.retryable is False
assert "malformed tool arguments" in error.message
_ORIGIN_KEYS = '"provider":"anthropic","api":"anthropic","model":"model-x"'
@pytest.mark.parametrize("report,expected", [
pytest.param(
"{" + _ORIGIN_KEYS + ',"source":"src_fixture01"}',
ServedHop(Origin("anthropic", "anthropic", "model-x"), "src_fixture01"),
id="exact",
),
pytest.param("{" + _ORIGIN_KEYS + "}", None, id="v11-without-source"),
pytest.param("{" + _ORIGIN_KEYS + ',"source":"src_fixture01","extra":"x"}', None, id="extra-key"),
pytest.param("{" + _ORIGIN_KEYS + ',"source":12345678}', None, id="non-string"),
pytest.param("{" + _ORIGIN_KEYS + ',"source":"src_short"}', None, id="bad-source-id"),
pytest.param(
"{" + _ORIGIN_KEYS + ',"source":"src_fixture01","source":"src_fixture02"}', None, id="duplicate-key",
),
])
def test_served_hop_report_is_exactly_origin_and_source(report: str, expected: ServedHop | None) -> None:
"""MH-VIBEY-002: only the exact v12 report verifies an origin; any other shape leaves it unverified."""
assert common_module.default_served_hop_resolver({"x-avibe-served-hop": report}) == expected
@pytest.mark.asyncio
@pytest.mark.parametrize("served_hop", [
None,
"invalid",
'{"provider":"anthropic","api":"anthropic","model":"model-x","source":"src_fixture01"}',
'{"provider":"anthropic","api":"anthropic","model":"model-x","source":"src_fixture02"}',
])
async def test_gateway_origin_sanitization_preserves_streamed_block_indexes(served_hop: str | None) -> None:
body = (
'data: {"type":"message_start","message":{"usage":{}}}\n\n'
'data: {"type":"content_block_start","index":0,"content_block":{"type":"redacted_thinking","data":"opaque"}}\n\n'
'data: {"type":"content_block_stop","index":0}\n\n'
'data: {"type":"content_block_start","index":1,"content_block":{"type":"thinking","signature":"sig"}}\n\n'
'data: {"type":"content_block_delta","index":1,"delta":{"type":"thinking_delta","thinking":"x"}}\n\n'
'data: {"type":"content_block_start","index":2,"content_block":{"type":"text","text":"visible"}}\n\n'
'data: {"type":"content_block_start","index":3,"content_block":{"type":"tool_use","id":"t","name":"read"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"tool_use"}}\n\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
headers = {"content-type": "text/event-stream"}
if served_hop is not None:
headers["x-avibe-served-hop"] = served_hop
return httpx.Response(200, headers=headers, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
request = _request("anthropic")
hop = replace(request.endpoint, source_id="src_fixture01", source_identity="sha256:fixture-account")
events = await _events(AnthropicAdapter(client, gateway=True), replace(request, endpoint=hop))
final = events[-1]
assert isinstance(final, Done)
verified = served_hop is not None and served_hop != "invalid"
assert len(final.message.content) == (4 if verified else 3)
assert any(isinstance(block, ThinkingBlock) and block.redacted for block in final.message.content) == verified
for event in events:
if isinstance(event, ThinkingDelta):
assert isinstance(final.message.content[event.index], ThinkingBlock)
assert final.message.content[event.index].text == "x"
elif isinstance(event, TextDelta):
assert final.message.content[event.index] == TextBlock("visible")
elif isinstance(event, ToolCallStart):
assert isinstance(final.message.content[event.index], ToolCallBlock)
assert final.message.content[event.index].id == event.id
if not verified:
assert all(not isinstance(block, ThinkingBlock) or block.signature is None for block in final.message.content)
assert all(not isinstance(block, ToolCallBlock) or block.signature is None for block in final.message.content)
# The message records the identity its opaque payloads go back to (cross-provider.md): the hop's, when the hop's
# own Source served it; none when another Source did, whose identity is unknown here.
own = verified and "src_fixture01" in served_hop
assert final.message.source_identity == ("sha256:fixture-account" if own else None)
class _Loader(MediaLoader):
def __init__(self) -> None:
self.tokens: list[str] = []
async def load(self, media_token: str) -> tuple[bytes, str]:
self.tokens.append(media_token)
return b"snapshot", "image/jpeg"
@pytest.mark.asyncio
async def test_media_loader_bytes_are_resolved_at_request_time() -> None:
captured: dict[str, Any] = {}
loader = _Loader()
def handler(request: httpx.Request) -> httpx.Response:
captured.update(json.loads(request.content))
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text='data: {"choices":[{"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n',
)
message = UserMessage((ImageBlock("image/png", "media-1", "x.png"),))
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
await _events(
OpenAIChatAdapter(client, media_loader=loader),
_request("openai_chat", messages=(message,), supports_images=True),
)
assert loader.tokens == ["media-1"]
image_part = captured["messages"][1]["content"][0]
assert image_part["image_url"]["url"] == f"data:image/jpeg;base64,{base64.b64encode(b'snapshot').decode()}"
class _FailingLoader(MediaLoader):
async def load(self, media_token: str) -> tuple[bytes, str]:
raise FileNotFoundError(media_token)
@pytest.mark.asyncio
@pytest.mark.parametrize("token,kind", [("missing", "unknown"), ("rate limit", "rate_limit"), ("server error", "server")])
async def test_media_loader_failure_is_a_provider_error(token, kind) -> None:
# Local preparation must not acquire replay/continuation permission from upstream-looking diagnostic text.
message = UserMessage((ImageBlock("image/png", token, "missing.png"),))
adapter = OpenAIChatAdapter(
httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(500))),
media_loader=_FailingLoader(),
)
events = await _events(
adapter,
_request("openai_chat", messages=(message,), supports_images=True),
)
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == kind
assert not events[-1].retryable and not events[-1].continuable
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol"),
[
(AnthropicAdapter, "anthropic"),
(OpenAIChatAdapter, "openai_chat"),
(OpenAIResponsesAdapter, "openai_responses"),
],
)
async def test_adapter_error_bodies_use_shared_json_redaction(
adapter_type: Any,
protocol: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
400,
text='{"detail":{"secretKey":"SKSECRET1","aws_secret_access_key":"AWSSECRET"}}',
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert "SKSECRET1" not in error.message
assert "AWSSECRET" not in error.message
@pytest.mark.asyncio
@pytest.mark.parametrize(
("event_type", "fields"),
[
("response.output_text.delta", {"output_index": 0}),
("response.refusal.delta", {"output_index": 0}),
("response.reasoning_summary_text.delta", {"output_index": 0}),
("response.reasoning_text.delta", {"output_index": 0}),
("response.function_call_arguments.delta", {"output_index": 0}),
],
)
async def test_responses_delta_events_require_string_payload(
event_type: str,
fields: dict[str, Any],
) -> None:
body = f"data: {json.dumps({'type': event_type, **fields})}\n\n"
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(
transport=httpx.MockTransport(handler)
) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert event_type in error.message
assert events[-1].retryable is False
@pytest.mark.asyncio
@pytest.mark.parametrize(
"payload",
[
{"output_index": 0, "delta": "lost"},
{"type": None, "output_index": 0, "delta": "lost"},
],
)
async def test_responses_named_event_requires_string_payload_type(
payload: dict[str, Any],
) -> None:
body = "event: response.output_text.delta\n" + f"data: {json.dumps(payload)}\n\n"
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=body,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "type" in error.message
@pytest.mark.asyncio
async def test_anthropic_named_event_requires_string_payload_type() -> None:
body = (
"event: content_block_delta\n"
'data: {"type":5,"index":0,"delta":{"type":"text_delta","text":"lost"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=body,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "type" in error.message
@pytest.mark.asyncio
async def test_anthropic_named_event_rejects_null_payload_type() -> None:
body = (
"event: content_block_delta\n"
'data: {"type":null,"index":0,"delta":{"type":"text_delta","text":"lost"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=body,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "type" in error.message
@pytest.mark.asyncio
async def test_pre_cancelled_request_yields_non_retryable_abort() -> None:
cancel = CancelToken()
cancel.cancel("user stopped")
adapter = OpenAIChatAdapter(httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(500))))
try:
events = await _events(adapter, _request("openai_chat"), cancel)
finally:
await adapter.aclose()
assert len(events) == 1
assert isinstance(events[0], ProviderError)
assert events[0].kind == "aborted"
assert events[0].retryable is False
@pytest.mark.asyncio
async def test_stream_error_after_a_delta_is_terminal_and_keeps_partial() -> None:
body = (
'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
"data: not-json\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.retryable is False
assert error.partial is not None
assert error.partial.content == (TextBlock(text="partial"),)
@pytest.mark.asyncio
async def test_http_413_is_classified_as_overflow() -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(413, text='{"error":{"message":"request too large"}}')
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "overflow"
assert error.status == 413
assert error.retryable is False
#: Anthropic's 400 for a ``max_tokens`` above the model's output limit: it names ``max_tokens`` too, but no smaller
#: context can fix it.
OUTPUT_LIMIT_REFUSAL = (
"max_tokens: 200000 > 128000, which is the maximum allowed number of output tokens for claude-opus-4-1-20250805"
)
@pytest.mark.asyncio
@pytest.mark.parametrize("envelope", [anthropic_error, model_hub_error], ids=["endpoint", "model-hub"])
@pytest.mark.parametrize(
("message", "kind"),
[(WINDOW_REFUSAL, "overflow"), (OUTPUT_LIMIT_REFUSAL, "invalid_request")],
ids=["window", "output-limit"],
)
async def test_anthropic_window_refusal_is_overflow_and_output_limit_refusal_is_not(
envelope: Any, message: str, kind: str,
) -> None:
# Models before Claude 4.5 refuse a request whose input plus max_tokens exceeds the window; later ones stop at the
# window instead. Through Model Hub the upstream's message reaches the adapter inside the gateway's copy and code.
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(400, json=envelope(message))
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
(error,) = events
assert isinstance(error, ProviderError)
assert (error.kind, error.status, error.retryable, error.partial) == (kind, 400, False, None)
@pytest.mark.asyncio
async def test_http_redirect_is_terminal_invalid_request() -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
307,
headers={"location": "https://other.test/v1"},
text='{"error":{"message":"password=super-secret"}}',
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.status == 307
assert error.retryable is False
assert "super-secret" not in error.message
@pytest.mark.asyncio
async def test_ipv6_endpoint_in_error_text_keeps_one_sanitized_terminal() -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
502,
text="upstream http://[::1]:8080/v1 refused",
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert len([event for event in events if isinstance(event, ProviderError)]) == 1
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is True
assert "http://[::1]:8080/v1" in error.message
@pytest.mark.asyncio
async def test_error_body_reader_applies_a_byte_cap() -> None:
response = httpx.Response(500, content=b"x" * 70_000)
body = await read_response_body(response, CancelToken(), max_bytes=64 * 1024)
assert body is not None
assert len(body.encode()) == 64 * 1024
class _StalledStream(httpx.AsyncByteStream):
def __init__(self, first_chunk: bytes | None = None) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
self.closed = asyncio.Event()
self.first_chunk = first_chunk or b'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
async def __aiter__(self):
self.started.set()
yield self.first_chunk
await self.release.wait()
async def aclose(self) -> None:
self.closed.set()
class _StalledFirstByteStream(httpx.AsyncByteStream):
def __init__(self) -> None:
self.started = asyncio.Event()
self.closed = asyncio.Event()
self.release = asyncio.Event()
async def __aiter__(self):
self.started.set()
await self.release.wait()
yield b"data: [DONE]\n\n"
async def aclose(self) -> None:
self.closed.set()
class _NonCooperativeReadStream(httpx.AsyncByteStream):
def __init__(self) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
self.cancel_seen = asyncio.Event()
self.cancel_count = 0
self.closed = asyncio.Event()
async def __aiter__(self):
self.started.set()
while not self.release.is_set():
try:
await self.release.wait()
except asyncio.CancelledError:
self.cancel_count += 1
self.cancel_seen.set()
yield b"data: [DONE]\n\n"
async def aclose(self) -> None:
self.closed.set()
self.release.set()
@pytest.mark.asyncio
@pytest.mark.parametrize("phase", ["headers", "first_byte", "idle", "after_output"])
async def test_shared_driver_bounds_each_wait_on_a_silent_provider(
monkeypatch: pytest.MonkeyPatch,
phase: str,
) -> None:
import core.agent_core.ai._common as common
monkeypatch.setattr(common, "PROVIDER_SILENCE_TIMEOUT_S", 0.01)
opened = asyncio.Event()
first_byte_stream = _StalledFirstByteStream()
idle_stream = _StalledStream(
None
if phase == "after_output"
else b'data: {"choices":[{"delta":{},"finish_reason":null}]}\n\n'
)
async def handler(_: httpx.Request) -> httpx.Response:
if phase == "headers":
opened.set()
await asyncio.Event().wait()
if phase == "first_byte":
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=first_byte_stream,
)
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=idle_stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(
_events(OpenAIChatAdapter(client), _request("openai_chat"))
)
if phase == "headers":
await opened.wait()
elif phase == "first_byte":
await first_byte_stream.started.wait()
else:
await idle_stream.started.wait()
events = await asyncio.wait_for(task, timeout=1)
# A connected provider that goes silent stalled; it is not a connection failure.
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "stalled"
assert error.retryable is False
if phase == "after_output":
# Output already reached the user: the stall ends the request with what streamed.
assert error.continuable is True
assert error.partial is not None
assert [block.text for block in error.partial.content] == ["partial"]
else:
# The whole bound passed without output: sent again, it would wait as long again.
assert error.continuable is False
assert error.partial is None
if phase == "first_byte":
assert first_byte_stream.closed.is_set()
elif phase != "headers":
assert idle_stream.closed.is_set()
class _SlowButAliveStream(httpx.AsyncByteStream):
def __init__(self, chunks: list[bytes], gap_s: float) -> None:
self.chunks = chunks
self.gap_s = gap_s
async def __aiter__(self):
for chunk in self.chunks:
await asyncio.sleep(self.gap_s)
yield chunk
@pytest.mark.asyncio
async def test_slow_but_alive_provider_is_bounded_per_silence_not_per_stream(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import core.agent_core.ai._common as common
# Response headers arrive long after connecting would have timed out, and
# the stream outlasts the silence bound, but no single silence reaches it.
monkeypatch.setattr(common, "CONNECT_TIMEOUT_S", 0.01)
monkeypatch.setattr(common, "PROVIDER_SILENCE_TIMEOUT_S", 0.5)
words = [f"w{index} " for index in range(12)]
chunks = [
b'data: ' + json.dumps({"choices": [{"delta": {"content": word}, "finish_reason": None}]}).encode() + b"\n\n"
for word in words
] + [b'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n', b"data: [DONE]\n\n"]
async def handler(_: httpx.Request) -> httpx.Response:
await asyncio.sleep(0.1)
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=_SlowButAliveStream(chunks, gap_s=0.05),
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await asyncio.wait_for(_events(OpenAIChatAdapter(client), _request("openai_chat")), timeout=5)
done = events[-1]
assert isinstance(done, Done)
assert done.message.content[0].text == "".join(words)
_RECOVERY_EXHAUSTED_BY_NETWORK = {
"type": "error",
"error": {"type": "model_hub_recovery_exhausted", "code": "model_hub_recovery_exhausted", "cause": "network"},
}
_ANSWERED = {
"anthropic": (
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":"ok"}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
"openai_chat": (
'data: {"choices":[{"delta":{"content":"ok"},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
),
"openai_responses": (
'data: {"type":"response.output_text.delta","delta":"ok"}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
),
}
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol"),
[(OpenAIResponsesAdapter, "openai_responses"), (OpenAIChatAdapter, "openai_chat"), (AnthropicAdapter, "anthropic")],
)
@pytest.mark.parametrize(
("answer", "bounds_waited", "continuable"),
[("served", 4, None), ("exhausted", 4, False), ("exhausted", 0, True)],
)
async def test_the_gateway_bounds_the_wait_for_headers_and_a_failure_that_outwaited_the_bound_is_final(
monkeypatch: pytest.MonkeyPatch, adapter_type, protocol: str, answer: str, bounds_waited: int, continuable: bool | None,
) -> None:
"""Model Hub sends response headers at the first model output, so a model thinking without streaming its thinking
holds them back for as long as it thinks. The gateway sees the upstream's bytes meanwhile and bounds that wait
itself; the driver does not. A failure that comes back only after the whole silence bound without output is not
sent again: it would most likely take as long again."""
import core.agent_core.ai._common as common
bound = 0.05
monkeypatch.setattr(common, "PROVIDER_SILENCE_TIMEOUT_S", bound)
async def handler(_: httpx.Request) -> httpx.Response:
await asyncio.sleep(bound * bounds_waited)
if answer == "exhausted":
return httpx.Response(424, json=_RECOVERY_EXHAUSTED_BY_NETWORK)
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=_ANSWERED[protocol])
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await asyncio.wait_for(_events(adapter_type(client, gateway=True), _request(protocol)), timeout=5)
terminal = events[-1]
if answer == "served":
assert isinstance(terminal, Done)
return
assert isinstance(terminal, ProviderError)
assert (terminal.kind, terminal.retryable, terminal.continuable) == ("network", False, continuable)
@pytest.mark.asyncio
@pytest.mark.parametrize("failure", [httpx.ConnectError, httpx.ConnectTimeout])
async def test_only_connecting_is_bounded_by_the_transport_and_its_failure_is_network(
failure: type[httpx.TransportError],
) -> None:
import core.agent_core.ai._common as common
seen: list[dict[str, float | None]] = []
def handler(request: httpx.Request) -> httpx.Response:
seen.append(request.extensions["timeout"])
raise failure("connect failed", request=request)
# A client of its own may carry read limits; the driver's request replaces them.
async with httpx.AsyncClient(transport=httpx.MockTransport(handler), timeout=5) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert seen == [{"connect": common.CONNECT_TIMEOUT_S, "read": None, "write": None, "pool": None}]
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "network"
assert error.retryable is True
@pytest.mark.asyncio
async def test_non_cooperative_stream_cancellation_join_does_not_block_driver(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import core.agent_core.ai._common as common
monkeypatch.setattr(common, "PROVIDER_SILENCE_TIMEOUT_S", 0.01)
monkeypatch.setattr(common, "CLEANUP_TIMEOUT_S", 0.01)
stream = _NonCooperativeReadStream()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(OpenAIChatAdapter(client), _request("openai_chat")))
await stream.started.wait()
events = await asyncio.wait_for(task, timeout=0.5)
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "stalled"
assert stream.cancel_seen.is_set()
assert stream.cancel_count == 1
assert stream.closed.is_set()
@pytest.mark.asyncio
async def test_cancellation_after_placeholder_slot_has_no_partial_message() -> None:
stream = _StalledStream(
b'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
cancel = CancelToken()
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(OpenAIResponsesAdapter(client), _request("openai_responses"), cancel))
await stream.started.wait()
cancel.cancel("cancelled after placeholder")
events = await asyncio.wait_for(task, timeout=1)
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "aborted"
assert error.partial is None
class _StalledErrorBody(httpx.AsyncByteStream):
def __init__(self) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
self.closed = asyncio.Event()
async def __aiter__(self):
self.started.set()
await self.release.wait()
yield b'{"error":{"message":"late"}}'
async def aclose(self) -> None:
self.closed.set()
class _ErrorBodyReadFailure(httpx.AsyncByteStream):
def __init__(self) -> None:
self.started = asyncio.Event()
self.closed = asyncio.Event()
async def __aiter__(self):
self.started.set()
raise OSError("error body read failed")
yield b""
async def aclose(self) -> None:
self.closed.set()
@pytest.mark.asyncio
async def test_redirect_error_body_read_failure_is_terminal_invalid_request(
monkeypatch: pytest.MonkeyPatch,
) -> None:
import core.agent_core.ai._common as common
monkeypatch.setattr(common, "PROVIDER_SILENCE_TIMEOUT_S", 0.01)
stream = _ErrorBodyReadFailure()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(307, stream=stream)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.status == 307
assert error.retryable is False
class _StalledLoader(MediaLoader):
def __init__(self) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
async def load(self, media_token: str) -> tuple[bytes, str]:
del media_token
self.started.set()
await self.release.wait()
return b"snapshot", "image/png"
class _StalledResolver:
def __init__(self) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
async def __call__(self, headers: Mapping[str, str]) -> ServedHop | None:
del headers
self.started.set()
await self.release.wait()
return ServedHop(Origin("served", "openai_chat", "served-model"), "src_served01")
class _CloseFailStream(httpx.AsyncByteStream):
def __init__(self, body: bytes) -> None:
self.body = body
async def __aiter__(self):
yield self.body
async def aclose(self) -> None:
raise OSError("close failed")
class _TrackCloseStream(httpx.AsyncByteStream):
def __init__(self, body: bytes) -> None:
self.body = body
self.closed = asyncio.Event()
async def __aiter__(self):
yield self.body
async def aclose(self) -> None:
self.closed.set()
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
@pytest.mark.parametrize("completion", ["same_cycle", "during_join", "detached"])
async def test_cancelled_header_arrival_disposes_the_unclaimed_response(
monkeypatch: pytest.MonkeyPatch, protocol: str, completion: str,
) -> None:
monkeypatch.setattr(common_module, "CLEANUP_TIMEOUT_S", 0.01)
cancel = CancelToken()
started = asyncio.Event()
release = asyncio.Event()
stream = _TrackCloseStream(b"")
async def handler(_: httpx.Request) -> httpx.Response:
started.set()
if completion == "same_cycle":
cancel.cancel("header race")
else:
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
if completion == "detached":
await release.wait()
return httpx.Response(200, stream=stream)
try:
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(adapter_class(protocol)(client), _request(protocol), cancel))
await started.wait()
cancel.cancel("header race")
events = await asyncio.wait_for(task, 1)
assert len(events) == 1
assert isinstance(events[0], ProviderError)
assert events[0].kind == "aborted"
release.set()
await asyncio.wait_for(stream.closed.wait(), 1)
finally:
release.set()
await stream.aclose()
class _StalledCloseStream(httpx.AsyncByteStream):
def __init__(self, body: bytes) -> None:
self.body = body
self.started = asyncio.Event()
self.release = asyncio.Event()
async def __aiter__(self):
yield self.body
async def aclose(self) -> None:
self.started.set()
await self.release.wait()
class _DelayedReadErrorStream(httpx.AsyncByteStream):
def __init__(self, first_chunk: bytes, order: list[str]) -> None:
self.first_chunk = first_chunk
self.order = order
self.read_started = asyncio.Event()
self.release = asyncio.Event()
async def __aiter__(self):
yield self.first_chunk
self.read_started.set()
await self.release.wait()
raise OSError("delayed transport read failed")
async def aclose(self) -> None:
self.order.append("close")
class _StalledCloseFailStream(httpx.AsyncByteStream):
def __init__(self) -> None:
self.started = asyncio.Event()
self.release = asyncio.Event()
async def __aiter__(self):
self.started.set()
await self.release.wait()
yield b""
async def aclose(self) -> None:
raise OSError("close failed while cancelled")
class _CancelAfterEofCloseFailStream(httpx.AsyncByteStream):
def __init__(self, body: bytes, cancel: CancelToken) -> None:
self.body = body
self.cancel = cancel
async def __aiter__(self):
yield self.body
self.cancel.cancel("cancelled after stream EOF")
async def aclose(self) -> None:
raise OSError("close failed after cancellation")
_CANCELLATION_CASES = (
(AnthropicAdapter, "anthropic", 'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"partial"}}\n\n'),
(OpenAIChatAdapter, "openai_chat", 'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":null}]}\n\n'),
(OpenAIResponsesAdapter, "openai_responses", 'data: {"type":"response.output_text.delta","delta":"partial"}\n\n'),
)
@pytest.mark.asyncio
@pytest.mark.parametrize(("adapter_type", "protocol", "stream_body"), _CANCELLATION_CASES)
@pytest.mark.parametrize("phase", ["open", "stream", "error_body", "media_load"])
async def test_all_adapters_cancel_at_every_http_lifecycle_phase(
adapter_type: Any,
protocol: str,
stream_body: str,
phase: str,
) -> None:
started = asyncio.Event()
stalled_stream = _StalledStream(stream_body.encode())
stalled_body = _StalledErrorBody()
stalled_loader = _StalledLoader()
async def handler(_: httpx.Request) -> httpx.Response:
if phase == "open":
started.set()
await asyncio.Event().wait()
if phase == "stream":
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stalled_stream,
)
if phase == "error_body":
return httpx.Response(500, stream=stalled_body)
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=stream_body,
)
cancel = CancelToken()
if phase == "media_load":
request = _request(
protocol,
messages=(UserMessage((ImageBlock("image/png", "media-1", "image.png"),)),),
supports_images=True,
)
else:
request = _request(protocol)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
adapter = adapter_type(
client,
media_loader=stalled_loader if phase == "media_load" else None,
)
task = asyncio.create_task(_events(adapter, request, cancel))
if phase == "open":
await started.wait()
elif phase == "stream":
await stalled_stream.started.wait()
elif phase == "error_body":
await stalled_body.started.wait()
else:
await stalled_loader.started.wait()
cancel.cancel(f"cancelled during {phase}")
events = await asyncio.wait_for(task, timeout=1)
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "aborted"
if phase == "stream":
assert stalled_stream.closed.is_set()
elif phase == "error_body":
assert stalled_body.closed.is_set()
@pytest.mark.asyncio
@pytest.mark.parametrize(("adapter_type", "protocol", "stream_body"), _CANCELLATION_CASES)
async def test_all_adapters_cancel_during_served_hop_resolution(
adapter_type: Any,
protocol: str,
stream_body: str,
) -> None:
resolver = _StalledResolver()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text=stream_body,
)
cancel = CancelToken()
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
adapter = adapter_type(client, served_hop_resolver=resolver, gateway=True)
task = asyncio.create_task(_events(adapter, _request(protocol), cancel))
await resolver.started.wait()
cancel.cancel("cancelled during served-hop resolution")
events = await asyncio.wait_for(task, timeout=1)
assert len(events) == 1
assert isinstance(events[0], ProviderError)
assert events[0].kind == "aborted"
@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["anthropic", "openai_chat", "openai_responses"])
@pytest.mark.parametrize(("status", "kind", "retryable"), [
(302, "invalid_request", False), (401, "auth", False), (403, "auth", False),
(429, "rate_limit", True), (503, "server", True),
])
@pytest.mark.parametrize("broken_body", [False, True])
async def test_http_error_classification_precedes_served_hop_resolution(
protocol: str, status: int, kind: str, retryable: bool, broken_body: bool,
) -> None:
# A resolver failure must not replace an already-known HTTP error. This
# also protects status/Retry-After when the error body itself cannot be read.
resolver_calls = []
closed = []
async def resolver(headers: Mapping[str, str]) -> ServedHop | None:
resolver_calls.append(headers)
raise RuntimeError("served-hop resolution failed")
class Body(httpx.AsyncByteStream):
async def __aiter__(self):
if broken_body:
raise OSError("body read failed")
yield b'{"error":{"message":"upstream unavailable"}}'
async def aclose(self) -> None:
closed.append(True)
async with httpx.AsyncClient(transport=httpx.MockTransport(
lambda _: httpx.Response(status, headers={"retry-after": "7"}, stream=Body()),
)) as client:
events = await _events(
adapter_class(protocol)(client, served_hop_resolver=resolver, gateway=True), _request(protocol),
)
assert len(events) == 1
error = events[0]
assert isinstance(error, ProviderError)
assert (error.kind, error.status, error.retryable) == (kind, status, retryable)
assert error.retry_after_s == 7
assert resolver_calls == []
assert closed
@pytest.mark.asyncio
@pytest.mark.parametrize(("adapter_type", "protocol", "body"), [
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
'data: [DONE]\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
),
])
async def test_close_failure_cannot_emit_a_second_terminal_event(
adapter_type: Any,
protocol: str,
body: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=_CloseFailStream(body.encode()),
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
terminals = [event for event in events if isinstance(event, (Done, ProviderError))]
assert len(terminals) == 1
@pytest.mark.asyncio
async def test_consumer_close_suppresses_response_close_failure() -> None:
body = b'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=_CloseFailStream(body),
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
stream = OpenAIChatAdapter(client).stream(_request("openai_chat"), CancelToken())
first = await anext(stream)
assert isinstance(first, TextDelta)
await stream.aclose()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
b'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
b'data: {"type":"message_stop"}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
b'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
b'data: [DONE]\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
b'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
),
],
)
async def test_stalled_response_close_is_bounded(
monkeypatch: pytest.MonkeyPatch,
adapter_type: Any,
protocol: str,
body: bytes,
) -> None:
import core.agent_core.ai._common as common
monkeypatch.setattr(common, "CLEANUP_TIMEOUT_S", 0.01)
stream = _StalledCloseStream(body)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await asyncio.wait_for(
_events(adapter_type(client), _request(protocol)),
timeout=1,
)
assert isinstance(events[-1], Done)
assert stream.started.is_set()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
b'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
b'data: {"type":"message_stop"}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
b'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
b'data: [DONE]\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
b'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
),
],
)
async def test_cancellation_during_response_cleanup_is_preserved(
adapter_type: Any,
protocol: str,
body: bytes,
) -> None:
stream = _StalledCloseStream(body)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(adapter_type(client), _request(protocol)))
await stream.started.wait()
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "status", "kind", "retryable"),
[
(AnthropicAdapter, "anthropic", 401, "auth", False),
(OpenAIChatAdapter, "openai_chat", 401, "auth", False),
(OpenAIResponsesAdapter, "openai_responses", 401, "auth", False),
(AnthropicAdapter, "anthropic", 429, "rate_limit", True),
(OpenAIChatAdapter, "openai_chat", 429, "rate_limit", True),
(OpenAIResponsesAdapter, "openai_responses", 429, "rate_limit", True),
],
)
async def test_error_body_read_failure_preserves_http_classification(
adapter_type: Any,
protocol: str,
status: int,
kind: str,
retryable: bool,
) -> None:
stream = _ErrorBodyReadFailure()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
status,
headers={"Retry-After": "7"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == kind
assert error.retryable is retryable
assert error.status == status
assert error.retry_after_s == 7
assert stream.closed.is_set()
@pytest.mark.asyncio
async def test_oversized_sse_pending_line_is_terminal_malformed_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(sse_module, "DEFAULT_MAX_PENDING_LINE_SIZE", 8)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
text="data: too-large-without-a-line-ending",
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.retryable is False
assert "SSE" in error.message
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
b'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
b'data: {"type":"message_stop"}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
b'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
b'data: [DONE]\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
b'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
),
],
)
async def test_consumer_close_after_terminal_closes_response(
adapter_type: Any,
protocol: str,
body: bytes,
) -> None:
stream = _TrackCloseStream(body)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
response_stream = adapter_type(client).stream(_request(protocol), CancelToken())
while not isinstance((event := await anext(response_stream)), (Done, ProviderError)):
pass
await response_stream.aclose()
assert stream.closed.is_set()
@pytest.mark.asyncio
async def test_task_cancellation_is_not_replaced_by_response_close_failure() -> None:
stream = _StalledCloseFailStream()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(OpenAIChatAdapter(client), _request("openai_chat")))
await stream.started.wait()
task.cancel()
with pytest.raises(asyncio.CancelledError):
await task
@pytest.mark.asyncio
async def test_chat_post_loop_cancel_with_close_failure_emits_one_terminal_event() -> None:
cancel = CancelToken()
body = b'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=_CancelAfterEofCloseFailStream(body, cancel),
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"), cancel)
terminals = [event for event in events if isinstance(event, (Done, ProviderError))]
assert len(terminals) == 1
assert isinstance(terminals[0], ProviderError)
assert terminals[0].kind == "aborted"
_DELAYED_READ_ERROR_CASES = (
(
AnthropicAdapter,
"anthropic",
b'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
b'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"partial"}}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
b'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":null}]}\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
b'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
b'data: {"type":"response.output_text.delta","output_index":0,"delta":"partial"}\n\n',
),
)
@pytest.mark.asyncio
@pytest.mark.parametrize(("adapter_type", "protocol", "first_chunk"), _DELAYED_READ_ERROR_CASES)
async def test_delayed_transport_read_emits_terminal_before_close(
adapter_type: Any,
protocol: str,
first_chunk: bytes,
) -> None:
order: list[str] = []
stream = _DelayedReadErrorStream(first_chunk, order)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
events: list[Any] = []
async def collect() -> None:
async for event in adapter.stream(_request(protocol), CancelToken()):
events.append(event)
if isinstance(event, ProviderError):
order.append("terminal")
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
adapter = adapter_type(client)
task = asyncio.create_task(collect())
await stream.read_started.wait()
stream.release.set()
await task
await adapter.aclose()
assert order == ["terminal", "close"]
terminals = [event for event in events if isinstance(event, (Done, ProviderError))]
assert len(terminals) == 1
assert isinstance(terminals[0], ProviderError)
assert terminals[0].kind == "network"
assert terminals[0].retryable is False
_WIRE_EVENT_CASES = (
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"ping"}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
"done",
"stop",
None,
None,
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"message_start","message":{"usage":{"input_tokens":4}}}\n\n'
'data: {"type":"error","error":{"type":"overloaded_error","message":"busy"}}\n\n'
),
"error",
None,
"overloaded",
True,
),
(
AnthropicAdapter,
"anthropic",
'event: error\ndata: {"type":"error","error":{"type":"authentication_error","message":"bad key"}}\n\n',
"error",
None,
"auth",
False,
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"message_start","message":{"usage":{"input_tokens":4}}}\n\n'
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"partial"}}\n\n'
'data: {"type":"error","error":{"type":"api_error","message":"failed"}}\n\n'
),
"error",
None,
"server",
True,
),
(
AnthropicAdapter,
"anthropic",
'data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","name":""}}\n\n',
"error",
None,
"invalid_request",
False,
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"error":{"type":"rate_limit_error","message":"slow down"}}\n\n',
"error",
None,
"rate_limit",
False,
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"error":{"type":"invalid_api_key","message":"bad key"}}\n\n',
"error",
None,
"auth",
False,
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"function_call":{"name":"read"}}}]}\n\n'
'data: {"choices":[{"delta":{"function_call":{"arguments":"{\\"path\\":"}}}]}\n\n'
'data: {"choices":[{"delta":{"function_call":{"arguments":"\\"x\\"}"}},"finish_reason":"function_call"}]}\n\n'
"data: [DONE]\n\n"
),
"done",
"tool_use",
None,
None,
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":""}}]}}]}\n\n',
"error",
None,
"invalid_request",
False,
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"content":"partial"}}]}\n\n'
'data: {"error":{"type":"server_error","message":"failed"}}\n\n'
),
"error",
None,
"server",
True,
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.failed","response":{"status":"failed","error":{"code":"server_error","message":"busy"},"usage":{"input_tokens":2}}}\n\n'
),
"error",
None,
"server",
True,
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.incomplete","response":{"status":"incomplete","error":{"code":"context_length_exceeded","message":"too long"},"usage":{"input_tokens":2}}}\n\n'
),
"error",
None,
"overflow",
True,
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","name":""}}\n\n',
"error",
None,
"invalid_request",
False,
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"error","error":{"code":"permission","message":"denied"}}\n\n',
"error",
None,
"auth",
False,
),
)
_WIRE_SHAPE_CASES = (
(
AnthropicAdapter,
"anthropic",
'data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","signature":7}}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{},"finish_reason":"stop"}],"usage":[]}\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.completed","response":{"status":"completed","usage":[]}}\n\n',
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":7}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":"text"}}\n\n'
'data: {"type":"content_block_delta","index":0,"delta":{"type":7}}\n\n'
),
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"message_delta","delta":{"stop_reason":7}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"content_block_start","index":0,'
'"content_block":{"type":"tool_use","id":7,"name":"read"}}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":"read"}]},'
'"finish_reason":"tool_calls"}]}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":7,'
'"function":{"name":"read","arguments":"{}"}}]},"finish_reason":"tool_calls"}]}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{"reasoning_content":7},"finish_reason":"stop"}]}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{"reasoning_details":{}},"finish_reason":"stop"}]}\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.completed","response":{"status":5}}\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.completed","response":'
'{"status":"completed","error":"boom"}}\n\n'
),
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.incomplete","response":'
'{"status":"incomplete","incomplete_details":"max_output_tokens"}}\n\n'
),
),
*(
(adapter, protocol, f"data: {json.dumps(frame)}\n\n")
for adapter, protocol, frame in [
(AnthropicAdapter, "anthropic", {"type": "error", "error": []}),
(AnthropicAdapter, "anthropic", {"type": "error", "error": {"message": 7}}),
(OpenAIChatAdapter, "openai_chat", {"choices": [{"usage": [], "finish_reason": "stop"}]}),
(OpenAIChatAdapter, "openai_chat", {"choices": [{"delta": {"reasoning": 2}, "finish_reason": "stop"}]}),
(OpenAIChatAdapter, "openai_chat", {"choices": [{"delta": {"reasoning_text": 2}, "finish_reason": "stop"}]}),
(OpenAIChatAdapter, "openai_chat", {"choices": [{"delta": {"tool_calls": [{"index": "0", "function": {"name": "read"}}]}, "finish_reason": "tool_calls"}]}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.created", "response": {"status": 2}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "error", "error": {"code": []}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "error", "error": {"type": 7}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.output_item.added", "item": {"type": 2}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.output_item.done", "item": {"type": "message", "content": {}}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.function_call_arguments.delta", "item_id": 2, "delta": "{}"}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.failed", "error": {"message": 7, "code": []}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.failed", "response": {"error": {"message": 7}}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.failed", "response": {"error": [], "usage": {"input_tokens": 4}}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.failed", "response": {"usage": []}}),
(OpenAIResponsesAdapter, "openai_responses", {"type": "response.incomplete", "response": {"status": "incomplete", "incomplete_details": {"reason": 2}}}),
]
),
)
@pytest.mark.asyncio
@pytest.mark.parametrize("source", ["http", "named", "data"])
@pytest.mark.parametrize(
("adapter_type", "protocol", "error", "kind"),
[
(AnthropicAdapter, "anthropic", {"type": "permission_error", "message": "denied"}, "auth"),
(OpenAIChatAdapter, "openai_chat", {"code": "invalid_api_key", "message": "denied"}, "auth"),
(OpenAIResponsesAdapter, "openai_responses", {"code": "server_error", "message": "busy"}, "server"),
],
)
async def test_provider_error_envelope_is_classified_identically_from_every_source(
source: str, adapter_type: Any, protocol: str, error: dict[str, Any], kind: str,
) -> None:
envelope = json.dumps({"type": "error", "error": error})
body = envelope if source == "http" else (
("event: error\n" if source == "named" else "") + f"data: {envelope}\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(400 if source == "http" else 200, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
assert len(events) == 1
assert isinstance(events[0], ProviderError)
assert events[0].kind == kind
assert events[0].retryable == (kind == "server")
# Failure frames a Model Hub relay writes into an HTTP 200 stream that has
# already started, so only the frame says what failed. sub2api writes the first
# three (`handler/gateway_handler.go` and `handler/openai_gateway_handler.go`
# `handleStreamingAwareErrorWithCode`, `handler/stream_error_event.go`
# `writeResponsesFailedSSE`, `mapUpstreamError` for the messages); the
# Anthropic frame is the one Claude Code reported through a relay. Some relays
# keep the `response.failed` event name but omit the duplicated JSON `type`
# (Model Hub's `stream_wire.py` accepts that envelope). OpenAI's own capacity
# shed sends `service_unavailable_error` in an error frame, which relays pass on.
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body", "message", "kind"),
[
(
AnthropicAdapter,
"anthropic",
'data: {"type":"error","error":{"type":"upstream_error","message":"Upstream request failed"}}\n\n',
"Upstream request failed",
"server",
),
(
OpenAIChatAdapter,
"openai_chat",
'event: error\ndata: {"error":{"type":"upstream_error",'
'"message":"Upstream authentication failed, please contact administrator"}}\n\n',
"Upstream authentication failed, please contact administrator",
"server",
),
(
OpenAIChatAdapter,
"openai_chat",
'event: error\ndata: {"error":{"type":"upstream_error",'
'"message":"Upstream service overloaded, please retry later"}}\n\n',
"Upstream service overloaded, please retry later",
"overloaded",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'event: response.failed\ndata: {"type":"response.failed","sequence_number":0,'
'"response":{"id":"resp_0f3c","object":"response","created_at":1760140800,"model":"gpt-5",'
'"status":"failed","output":[],'
'"error":{"code":"upstream_error","message":"Upstream service temporarily unavailable"}}}\n\n',
"Upstream service temporarily unavailable",
"server",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'event: response.failed\ndata: {"response":{"error":{"code":"upstream_error",'
'"message":"response protection is unavailable"}}}\n\n',
"response protection is unavailable",
"server",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'event: response.failed\ndata: {"type":"response.failed","response":{"status":"failed",'
'"error":{"code":"service_unavailable_error","message":"Service temporarily unavailable"}}}\n\n',
"Service temporarily unavailable",
"server",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'event: error\ndata: {"type":"error","error":{"type":"service_unavailable_error",'
'"message":"Our servers are currently overloaded. Please try again later."}}\n\n',
"Our servers are currently overloaded. Please try again later.",
"overloaded",
),
# CLIProxyAPI, Model Hub's engine, byte for byte: its Claude type for an upstream 408 or 504, and its
# Responses class for an upstream stream that closed before its terminal.
(
AnthropicAdapter,
"anthropic",
'event: error\ndata: {"type":"error","error":{"type":"timeout_error","message":"upstream timeout"}}\n\n',
"upstream timeout",
"server",
),
(
OpenAIResponsesAdapter,
"openai_responses",
'\nevent: error\ndata: {"type":"error","error":{"code":"request_timeout","message":"stream error: stream '
'disconnected before completion: stream closed before response.completed","param":null,'
'"type":"server_error"},"sequence_number":1}\n\n',
"stream error: stream disconnected before completion: stream closed before response.completed",
"server",
),
],
)
async def test_a_relay_failure_frame_before_output_is_a_retryable_server_failure(
adapter_type: Any, protocol: str, body: str, message: str, kind: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
assert len(events) == 1
error = events[0]
assert isinstance(error, ProviderError)
assert (error.kind, error.retryable) == (kind, True)
assert error.message == message
# Reasoning streamed, then the response ended in an error frame: the shape Model Hub writes when an upstream stream
# breaks after output (its code is a copy key, its message localized copy).
_REASONING_THEN = (
'event: response.output_item.added\ndata: {"type":"response.output_item.added","output_index":0,'
'"item":{"type":"reasoning","id":"rs_1","summary":[]}}\n\n'
'event: response.reasoning_summary_text.delta\ndata: {"type":"response.reasoning_summary_text.delta",'
'"output_index":0,"delta":"**Checking catalog model metadata**"}\n\n'
"event: error\ndata: {}\n\n"
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("message", "kept"),
[
(
"The upstream stream was interrupted. Authorization: Bearer sk-proj-partial0secret0value",
"The upstream stream was interrupted. Authorization: Bearer [redacted]",
),
("x" * 5000, "x" * 999 + "\u2026"),
],
ids=["redacted", "bounded"],
)
async def test_a_failed_response_keeps_its_error_on_the_partial_it_commits(message: str, kept: str) -> None:
"""C-1 ``error_message``: the committed partial says why the response ended, redacted, then bounded."""
frame = json.dumps({"type": "error", "code": "modelHub.errors.stream_interrupted", "message": message})
body = _REASONING_THEN.replace("{}", frame)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError) and error.kind == "interrupted"
assert error.partial is not None and error.partial.stop_reason == "error"
assert error.partial.error_message == kept
# Model Hub's frame for an upstream stream that ended after output without a terminal: its code is the gateway's
# copy key, its message localized copy (``stream_wire.py`` renders the Responses and Chat frames).
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "frame"),
[
(
OpenAIResponsesAdapter,
"openai_responses",
{"type": "error", "code": "modelHub.errors.stream_interrupted", "message": "供应商响应已中断。", "param": None},
),
(
OpenAIChatAdapter,
"openai_chat",
{
"object": "chat.completion.chunk",
"type": "error",
"error": {"type": "server_error", "code": "modelHub.errors.stream_interrupted", "message": "中断"},
"choices": [],
},
),
],
)
async def test_model_hubs_stream_interrupted_frame_is_the_interrupted_kind(adapter_type, protocol, frame) -> None:
body = f"event: error\ndata: {json.dumps(frame, ensure_ascii=False)}\n\n"
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert (error.kind, error.retryable) == ("interrupted", True)
# Model Hub's ending once its recovery window ran out (``turn_gateway.py``): an HTTP 424 whose envelope names the
# failure the hub waited out. The loop must not replay that same request; a pre-output server/network cause may still
# permit Vibey's safe transcript-only continuation, while the copy must name the failure rather than blame the request.
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol"),
[(OpenAIResponsesAdapter, "openai_responses"), (OpenAIChatAdapter, "openai_chat"), (AnthropicAdapter, "anthropic")],
)
@pytest.mark.parametrize(
("cause", "kind", "continuable"),
[
("server_error", "server", True),
("network", "network", True),
("rate_limited", "rate_limit", False),
("quota_exhausted", "rate_limit", False),
(None, "server", False),
],
)
async def test_model_hubs_recovery_exhausted_ending_names_its_cause(
adapter_type, protocol, cause, kind, continuable
) -> None:
ending = {
"type": "model_hub_recovery_exhausted",
"code": "model_hub_recovery_exhausted",
"message": "Automatic recovery has ended. Try again or choose another model.",
}
if cause is not None:
ending["cause"] = cause
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(424, json={"type": "error", "error": ending})
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert (error.kind, error.retryable, error.continuable) == (kind, False, continuable)
# CLIProxyAPI nests the class under `error`; OpenAI's reference carries it at the root.
_SERVER_ERROR_FRAMES = {
"cpa-nested": 'data: {"type":"error","error":{"code":"request_timeout","message":"stream error: stream '
'disconnected before completion: stream closed before response.completed","param":null,"type":"server_error"},'
'"sequence_number":3}\n\n',
"root": 'data: {"type":"error","code":"server_error","message":"failed"}\n\n',
}
@pytest.mark.asyncio
@pytest.mark.parametrize("frame", sorted(_SERVER_ERROR_FRAMES))
@pytest.mark.parametrize(
("streamed", "flagged"),
[
("", False),
('data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"plan"}\n\n', True),
(
'data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"plan"}\n\n'
'data: {"type":"response.output_item.added","output_index":1,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":1,"delta":"Hi"}\n\n',
False,
),
],
ids=["nothing", "thinking-alone", "thinking-then-text"],
)
async def test_only_a_drop_after_thinking_alone_is_marked_after_thinking(
streamed: str, flagged: bool, frame: str
) -> None:
"""C-2: ``after_thinking`` marks a transient drop after thinking alone, which is then ``retryable`` once."""
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
+ streamed
+ _SERVER_ERROR_FRAMES[frame]
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError) and error.kind == "server"
assert error.after_thinking is flagged
# Before any output a server error was always retryable; after text it never is.
assert error.retryable is (flagged or not streamed)
assert error.continuable is True
@pytest.mark.parametrize("output", ["none", "thinking", "text"])
@pytest.mark.parametrize(
"failure",
[
"classified transient",
"explicit transient",
"eof",
"stalled",
"recovery exhausted",
"pin refusal",
"malformed",
"unknown wire",
"local exception",
],
)
def test_stream_error_producers_preserve_continuation_permission(output, failure):
"""C-2: terminal sanitization must not resurrect exhausted/refused/local failures after thinking.
Prior tests covered kinds and replay only, so the outer adapter could undo their decision.
"""
from core.agent_core.ai._common import PinUnavailable, pin_refusal
from core.agent_core.ai.errors import ProviderStalled
endpoint = _request("openai_responses").endpoint
assembler = StreamAssembler(origin=Origin("test", "openai_responses", "model"), protocol="openai_responses")
if output == "thinking":
assembler.thinking_delta(0, "reasoning")
elif output == "text":
assembler.text_delta(0, "partial")
if failure == "classified transient":
error = assembler.error("busy", status=503)
elif failure == "explicit transient":
error = assembler.error("busy", kind="server")
elif failure == "eof":
error = assembler.incomplete()
elif failure == "stalled":
error = assembler.exception(ProviderStalled(600))
elif failure == "recovery exhausted":
error = assembler.error(json.dumps({"error": {"code": "model_hub_recovery_exhausted", "cause": "server_error"}}))
elif failure == "pin refusal":
error = pin_refusal(assembler, endpoint, PinUnavailable())
elif failure == "malformed":
error = assembler.error("bad wire shape", kind="invalid_request")
elif failure == "unknown wire":
error = assembler.error("unclassified failure")
else:
error = assembler.exception(RuntimeError("local failure"))
terminal = assembler.terminal(error)
expected_continuable = failure in {"classified transient", "explicit transient", "eof", "stalled"}
if failure == "recovery exhausted":
expected_continuable = output == "none"
assert terminal.continuable is expected_continuable
expected_retry = (
expected_continuable and failure != "recovery exhausted" and output != "text"
) or (failure == "unknown wire" and output == "thinking")
assert terminal.retryable is expected_retry
assert terminal.after_thinking is (expected_retry and output == "thinking")
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body", "outcome", "stop_reason", "error_kind", "partial"),
_WIRE_EVENT_CASES,
)
async def test_wire_event_matrix_has_one_explicit_outcome(
adapter_type: Any,
protocol: str,
body: str,
outcome: str,
stop_reason: str | None,
error_kind: str | None,
partial: bool | None,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
terminals = [event for event in events if isinstance(event, (Done, ProviderError))]
assert len(terminals) == 1
terminal = terminals[0]
if outcome == "done":
assert isinstance(terminal, Done)
assert terminal.message.stop_reason == stop_reason
else:
assert isinstance(terminal, ProviderError)
assert terminal.kind == error_kind
if partial:
assert terminal.partial is not None
elif partial is False:
assert terminal.partial is None
@pytest.mark.asyncio
@pytest.mark.parametrize(("adapter_type", "protocol", "body"), _WIRE_SHAPE_CASES)
async def test_known_wire_fields_with_wrong_types_are_terminal_malformed(
adapter_type: Any,
protocol: str,
body: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
terminals = [event for event in events if isinstance(event, (Done, ProviderError))]
assert len(terminals) == 1
assert isinstance(terminals[0], ProviderError)
assert terminals[0].kind == "invalid_request"
@pytest.mark.asyncio
async def test_cancellation_during_a_stalled_read_ends_with_aborted_error() -> None:
stream = _StalledStream()
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=stream)
cancel = CancelToken()
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(OpenAIChatAdapter(client), _request("openai_chat"), cancel))
await stream.started.wait()
cancel.cancel("cancelled by test")
events = await asyncio.wait_for(task, timeout=1)
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "aborted"
assert not any(isinstance(event, Done) for event in events)
@pytest.mark.asyncio
async def test_cancellation_during_stream_open_ends_with_aborted_error() -> None:
started = asyncio.Event()
async def handler(_: httpx.Request) -> httpx.Response:
started.set()
await asyncio.Event().wait()
raise AssertionError("unreachable")
cancel = CancelToken()
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
task = asyncio.create_task(_events(OpenAIChatAdapter(client), _request("openai_chat"), cancel))
await started.wait()
cancel.cancel("cancelled while opening")
events = await asyncio.wait_for(task, timeout=1)
assert events == [
ProviderError(
kind="aborted",
message="cancelled while opening",
retryable=False,
)
]
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
'data: {"type":"content_block_start","index":0,"content_block":{"type":"text"}}\n\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"partial"}}\n\n',
),
(
OpenAIChatAdapter,
"openai_chat",
'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":null}]}\n\n',
),
(
OpenAIResponsesAdapter,
"openai_responses",
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_text.delta","output_index":0,"delta":"partial"}\n\n',
),
],
)
async def test_eof_without_protocol_terminal_is_not_success(
adapter_type: Any,
protocol: str,
body: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
assert isinstance(events[-1], ProviderError)
assert events[-1].retryable is False
assert events[-1].continuable is True
assert events[-1].partial is not None
assert not any(isinstance(event, Done) for event in events)
@pytest.mark.asyncio
async def test_empty_reasoning_slot_does_not_disable_network_retry() -> None:
stream = _CloseFailStream(
b'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
stream=stream,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "network"
assert error.retryable is True
assert error.partial is None
@pytest.mark.asyncio
async def test_adapter_error_redaction_preserves_json_classification() -> None:
body = '{"details":[{"url":"https://model.test/v1?project=secret"}]}'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(413, text=body)
request = ModelRequest(
endpoint=ModelEndpoint(
protocol="openai_chat",
base_url="https://user:[email protected]/v1?token=secret",
model_id="model-x",
token="token",
provider="openai",
),
system="system",
messages=(UserMessage((TextBlock(text="hello"),)),),
tools=(),
max_tokens=128,
)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), request)
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "overflow"
parsed = json.loads(error.message)
assert parsed["details"][0]["url"] == "https://model.test/v1"
assert "password" not in error.message
assert "project=secret" not in error.message
@pytest.mark.asyncio
@pytest.mark.parametrize(
"body",
[
'{"error":{"message":"x-goog-api-key: google-secret"}}',
'{"error":{"x-goog-api-key":"google-secret"}}',
"provider failed: x-goog-api-key=google-secret",
],
)
async def test_shared_error_parser_redacts_google_api_key(body: str) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(500, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert "google-secret" not in error.message
@pytest.mark.asyncio
@pytest.mark.parametrize(
("adapter_type", "protocol", "body"),
[
(
AnthropicAdapter,
"anthropic",
(
'data: {"type":"message_start","message":{"usage":{"input_tokens":-1}}}\n\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n'
),
),
(
OpenAIChatAdapter,
"openai_chat",
(
'data: {"choices":[{"delta":{"content":"ok"},"finish_reason":"stop"}],'
'"usage":{"prompt_tokens":-1}}\n\n'
"data: [DONE]\n\n"
),
),
(
OpenAIResponsesAdapter,
"openai_responses",
(
'data: {"type":"response.completed","response":{"status":"completed",'
'"output":[],"usage":{"input_tokens":"2"}}}\n\n'
),
),
],
)
async def test_adapters_reject_malformed_present_usage_counters(
adapter_type: Any,
protocol: str,
body: str,
) -> None:
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(adapter_type(client), _request(protocol))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert "usage" in error.message.lower()
@pytest.mark.asyncio
async def test_chat_requires_finish_reason_before_done_marker() -> None:
body = "data: [DONE]\n\n"
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "network"
assert events[-1].retryable is True
assert events[-1].partial is None
@pytest.mark.asyncio
async def test_chat_prompt_feedback_without_candidates_is_incomplete() -> None:
body = (
'data: {"promptFeedback":{"blockReason":"SAFETY"}}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "network"
assert error.retryable is True
assert error.partial is None
@pytest.mark.asyncio
async def test_chat_finish_reason_can_terminate_at_eof_without_done_marker() -> None:
body = 'data: {"choices":[{"delta":{"content":"ok"},"finish_reason":"stop"}]}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "stop"
@pytest.mark.asyncio
async def test_chat_max_tokens_maps_to_length() -> None:
body = (
'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":"max_tokens"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "length"
@pytest.mark.asyncio
@pytest.mark.parametrize("native_reason", ["safety", "recitation"])
async def test_chat_native_safety_overrides_stop(native_reason: str) -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"name":"read","arguments":"{}"}}]},"finish_reason":null}]}\n\n'
f'data: {json.dumps({"choices": [{"delta": {}, "finish_reason": "stop", "native_finish_reason": native_reason}]})}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "safety"
assert len(final.message.tool_calls) == 1
@pytest.mark.asyncio
async def test_chat_unknown_finish_reason_is_canonical_error() -> None:
body = 'data: {"choices":[{"delta":{},"finish_reason":"future_reason"}]}\n\ndata: [DONE]\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "error"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"content",
[
[7],
[{"type": "output_text", "text": 7}],
[{"type": "refusal", "refusal": 7}],
],
)
async def test_responses_rejects_malformed_message_content_parts(content: list[Any]) -> None:
body = (
'data: {"type":"response.output_item.done","output_index":0,'
+ json.dumps(
{
"item": {
"type": "message",
"content": content,
}
},
ensure_ascii=False,
)[1:]
+ "\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.retryable is False
@pytest.mark.asyncio
async def test_responses_rejects_malformed_terminal_message_content() -> None:
body = (
'data: {"type":"response.completed","response":{"status":"completed",'
'"output":[{"type":"message","content":[{"type":"output_text","text":7}]}]}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
assert error.retryable is False
@pytest.mark.asyncio
async def test_anthropic_message_stop_without_reason_is_terminal_error() -> None:
body = 'data: {"type":"message_stop"}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "invalid_request"
@pytest.mark.asyncio
async def test_anthropic_stop_reason_allows_eof_without_message_stop_like_pi() -> None:
body = 'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
final = events[-1]
assert isinstance(final, Done)
assert final.message.stop_reason == "stop"
@pytest.mark.asyncio
async def test_anthropic_message_start_requires_message_stop_even_with_stop_reason() -> None:
body = (
'event: message_start\n'
'data: {"type":"message_start","message":{"usage":{"input_tokens":7}}}\n\n'
'event: message_delta\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "network"
assert error.retryable is True
assert error.partial is not None
assert error.partial.usage == Usage(input_tokens=7)
@pytest.mark.asyncio
async def test_anthropic_malformed_data_only_frame_is_ignored_like_pi() -> None:
body = (
"data: not-json\n\n"
'event: message_delta\n'
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'event: message_stop\n'
'data: {"type":"message_stop"}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(AnthropicAdapter(client), _request("anthropic"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "stop"
@pytest.mark.asyncio
async def test_chat_refusal_stop_is_not_overwritten_by_later_stop_reason() -> None:
body = (
'data: {"choices":[{"delta":{"refusal":"cannot help"},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.stop_reason == "refusal"
@pytest.mark.asyncio
async def test_responses_message_output_item_done_backfills_text() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"done"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert [event.delta for event in events if isinstance(event, TextDelta)] == ["done"]
assert events[-1].message.content == (TextBlock("done"),)
@pytest.mark.asyncio
async def test_responses_reasoning_snapshot_emits_unseen_suffix() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_text.delta","output_index":0,"delta":"plan"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"plan"},{"type":"summary_text","text":"final"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.delta for event in events if isinstance(event, ThinkingDelta)] == [
"plan",
"\n\nfinal",
]
assert isinstance(events[-1], Done)
assert events[-1].message.content == (ThinkingBlock("plan\n\nfinal"),)
@pytest.mark.asyncio
async def test_chat_typed_model_hub_error_frame_is_terminal() -> None:
body = (
'data: {"choices":[{"delta":{"content":"partial"},"finish_reason":null}]}\n\n'
'data: {"object":"chat.completion.chunk","type":"error","error":{"type":"server_error","message":"failed"},"choices":[]}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "server"
assert events[-1].partial is not None
assert events[-1].partial.content == (TextBlock("partial"),)
@pytest.mark.asyncio
async def test_responses_snapshot_output_counts_as_streamed_partial() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_1"}}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","content":[{"type":"output_text","text":"snapshot"}]}}\n\n'
'data: {"type":"response.failed","response":{"status":"failed","error":{"code":"server_error","message":"failed"}}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "server"
assert error.retryable is False
assert error.partial is not None
assert error.partial.content == (TextBlock("snapshot"),)
@pytest.mark.asyncio
@pytest.mark.parametrize("field", ["content", "refusal"])
async def test_chat_known_text_fields_reject_wrong_types(field: str) -> None:
delta = {field: 7}
body = f'data: {json.dumps({"choices": [{"delta": delta, "finish_reason": "stop"}]})}\n\n'
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
error = events[-1]
assert isinstance(error, ProviderError)
assert error.kind == "invalid_request"
@pytest.mark.asyncio
async def test_chat_tool_call_without_integer_index_uses_native_id_alias() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"id":"call_1","function":{"name":"read"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"id":"call_1","function":{"arguments":"{\\"path\\":\\"x\\"}"}}]},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].id == "call_1"
assert events[-1].message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_chat_null_optional_fields_are_absent() -> None:
body = (
'data: {"choices":[{"delta":{"content":"ok","tool_calls":null,"function_call":null},"finish_reason":"stop"}],"usage":null}\n\n'
'data: {"choices":null,"usage":null}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (TextBlock("ok"),)
@pytest.mark.asyncio
async def test_chat_tool_call_without_name_is_terminal_invalid_request() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"arguments":"{}"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert isinstance(events[-1], ProviderError)
assert events[-1].kind == "invalid_request"
assert "tool call name is missing" in events[-1].message
@pytest.mark.asyncio
async def test_chat_arguments_before_name_are_emitted_after_tool_start() -> None:
body = (
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","function":{"arguments":"{\\"path\\":\\"x\\"}"}}]},"finish_reason":null}]}\n\n'
'data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"name":"read","arguments":null}}]},"finish_reason":"tool_calls"}]}\n\n'
"data: [DONE]\n\n"
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIChatAdapter(client), _request("openai_chat"))
assert [event.arguments_delta for event in events if isinstance(event, ToolCallDelta)] == [
'{"path":"x"}'
]
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {"path": "x"}
@pytest.mark.asyncio
async def test_responses_non_prefix_arguments_done_emits_no_duplicate_delta() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read"}}\n\n'
'data: {"type":"response.function_call_arguments.delta","output_index":0,"delta":"{\\"a\\":1"}\n\n'
'data: {"type":"response.function_call_arguments.done","output_index":0,"arguments":"{\\"a\\": 1}"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_1","name":"read","arguments":"{\\"a\\": 1}"}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert [event.arguments_delta for event in events if isinstance(event, ToolCallDelta)] == ['{"a":1']
assert isinstance(events[-1], Done)
assert events[-1].message.tool_calls[0].arguments == {"a": 1}
@pytest.mark.asyncio
async def test_responses_summary_done_replaces_streaming_separator_after_late_delta() -> None:
body = (
'data: {"type":"response.output_item.added","output_index":0,"item":{"type":"reasoning","id":"rs_1"}}\n\n'
'data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"p1"}\n\n'
'data: {"type":"response.reasoning_summary_part.done","output_index":0}\n\n'
'data: {"type":"response.reasoning_summary_text.delta","output_index":0,"delta":"p2"}\n\n'
'data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","encrypted_content":"enc","summary":[{"type":"summary_text","text":"p1"},{"type":"summary_text","text":"p2"}]}}\n\n'
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n'
)
def handler(_: httpx.Request) -> httpx.Response:
return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=body)
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
events = await _events(OpenAIResponsesAdapter(client), _request("openai_responses"))
assert isinstance(events[-1], Done)
assert events[-1].message.content == (
ThinkingBlock(
"p1\n\np2",
'{"type":"reasoning","id":"rs_1","encrypted_content":"enc","summary":[{"type":"summary_text","text":"p1"},{"type":"summary_text","text":"p2"}]}',
),
)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("protocol", "path", "body"),
[
(
"anthropic",
"/v1/messages",
'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'
'data: {"type":"message_stop"}\n\n',
),
(
"openai_chat",
"/v1/chat/completions",
'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n',
),
(
"openai_responses",
"/v1/responses",
'data: {"type":"response.completed","response":{"status":"completed"}}\n\n',
),
],
)
async def test_gateway_adapters_reach_loopback_past_configured_proxies(
monkeypatch: pytest.MonkeyPatch, protocol: str, path: str, body: str,
) -> None:
"""The gateway is loopback: a proxy from the environment never carries its requests."""
import socket
from aiohttp import web
with socket.socket() as unused:
unused.bind(("127.0.0.1", 0))
dead_proxy = f"http://127.0.0.1:{unused.getsockname()[1]}"
for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"):
monkeypatch.setenv(name, dead_proxy)
for name in ("NO_PROXY", "no_proxy"):
monkeypatch.delenv(name, raising=False)
seen: list[str] = []
async def gateway(request: web.Request) -> web.Response:
seen.append(request.path)
return web.Response(text=body, headers={"Content-Type": "text/event-stream"})
app = web.Application()
app.router.add_post(path, gateway)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "127.0.0.1", 0)
await site.start()
port = site._server.sockets[0].getsockname()[1]
adapter = adapter_class(protocol)(gateway=True)
try:
events = await _events(adapter, _request(protocol, base_url=f"http://127.0.0.1:{port}/v1"))
finally:
await adapter.aclose()
await runner.cleanup()
assert seen == [path]
assert isinstance(events[-1], Done)