litellm/tests/e2e/claude_code/stream_events.py
Yuneng Jiang d072d5181b
test(e2e): say what the model did when a Claude Code web search run skips WebSearch
The web search compat tests only reported that no WebSearch tool_use was seen, so a
red Opus 4.7 Bedrock run left no trace of what the model did instead. The failure
message now lists every tool call in order, the result event's stop reason and turn
count, and the start of the final reply, read from the CLI's stream-json events.
2026-11-24 15:15:00 -08:00

70 lines
2 KiB
Python

from __future__ import annotations
from collections.abc import Iterator, Mapping, Sequence
from typing import Annotated, Final, Literal
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter
class _Frozen(BaseModel):
model_config = ConfigDict(frozen=True)
class TextBlock(_Frozen):
type: Literal["text"]
text: str
class ToolUseBlock(_Frozen):
type: Literal["tool_use"]
name: str
class OtherBlock(_Frozen):
type: str | None = None
ContentBlock = Annotated[TextBlock | ToolUseBlock | OtherBlock, Field(union_mode="left_to_right")]
class AssistantMessage(_Frozen):
content: tuple[ContentBlock, ...] = ()
class AssistantEvent(_Frozen):
type: Literal["assistant"]
message: AssistantMessage
class ResultEvent(_Frozen):
type: Literal["result"]
stop_reason: str | None = None
num_turns: int | None = None
result: str | None = None
class OtherEvent(_Frozen):
type: str | None = None
StreamEvent = Annotated[AssistantEvent | ResultEvent | OtherEvent, Field(union_mode="left_to_right")]
_EVENTS: Final = TypeAdapter(tuple[StreamEvent, ...])
def _assistant_blocks(events: tuple[StreamEvent, ...]) -> Iterator[ContentBlock]:
for event in events:
if isinstance(event, AssistantEvent):
yield from event.message.content
def describe_run(events: Sequence[Mapping[str, object]], *, reply_chars: int = 300) -> str:
parsed: Final = _EVENTS.validate_python(events)
tool_calls: Final = tuple(block.name for block in _assistant_blocks(parsed) if isinstance(block, ToolUseBlock))
calls: Final = ", ".join(tool_calls) or "none"
result: Final = next((event for event in parsed if isinstance(event, ResultEvent)), None)
if result is None:
return f"tool calls: {calls}; no result event"
reply: Final = result.result or ""
shown: Final = reply if len(reply) <= reply_chars else f"{reply[:reply_chars]}..."
return f"tool calls: {calls}; stop_reason={result.stop_reason}; turns={result.num_turns}; reply={shown!r}"