Same Response, Two Code Paths
Replay testing of streaming parity in LLM SDKs
Muhammad Ahmad. mahmadmanzoor.com
Preprint, not peer reviewed.
Summary
Applications call language models through SDKs. A provider can send a response as one JSON document or as a stream of events, and the SDK has separate code for each.
This study replays the same real recorded responses both ways through four SDKs (Vercel AI SDK, LiteLLM, LangChain and Pydantic AI) and checks that the two results agree. It also checks that writing the same stream to the network in six different ways changes nothing. Nothing needs to be known about the correct result: the two results only have to equal each other. Every difference was traced to the code that causes it and then checked in a second, independent pass.
How it works
Each recorded stream is replayed twice. Both calls go to the same SDK, and the two results are compared.
Key numbers
- 1,158recorded streams
- 7open-source projects they come from
- 4SDKs tested
- 46,320SDK runs
- 93 of 4,592stream versus non-stream comparisons where the SDK gives two different results (2.0%)
- 415 of 27,792transport runs that differ
- 20distinct causes traced to code
Stream versus non-stream
| SDK | OpenAI Chat Completions | OpenAI Responses | Anthropic Messages | Gemini | All | Caused by the test rig |
|---|---|---|---|---|---|---|
| Vercel AI SDK | 0 / 149 | 0 / 280 | 5 / 639 | 0 / 80 | 5 / 1,148 (0.4%) | 0 |
| LiteLLM | 4 / 149 | 20 / 280 | 3 / 640 | 8 / 80 | 35 / 1,149 (3.0%) | 40 |
| LangChain | 0 / 149 | 1 / 280 | 46 / 639 | 5 / 80 | 52 / 1,148 (4.5%) | 21 |
| Pydantic AI | 0 / 149 | 0 / 279 | 1 / 639 | 0 / 80 | 1 / 1,147 (0.1%) | 2 |
A further 63 differences were caused by the test rig, not by the SDK, and are not counted above. 23 came from settings of the original requests that the rig did not replay (beta features for Anthropic, structured output for OpenAI, one web-search option); they disappear when the setting is supplied. 40 came from streams recorded without token usage: the body derived from them has no usage either, which OpenAI's real non-streaming endpoint always sends. On those, LiteLLM reports zero tokens when not streaming and a local estimate when streaming.
Same stream, written differently
| SDK | whole | bytes | utf8 | crlf | comment | nospace |
|---|---|---|---|---|---|---|
| Vercel AI SDK | 0 | 0 | 0 | 0 | 0 | 0 |
| LiteLLM | 0 | 0 | 0 | 0 | 83 | 0 |
| LangChain | 0 | 0 | 0 | 0 | 83 | 83 |
| Pydantic AI | 0 | 0 | 0 | 0 | 83 | 83 |
whole: one write; bytes: cut at random bytes; utf8: cut inside multi-byte characters; crlf: CRLF line endings; comment: an SSE comment line before every event; nospace: no space after the field colon.
Every nonzero cell is the Gemini recordings.
Where an OpenAI refusal ends up
| SDK | Chat Completions not streamed | Chat Completions streamed | Responses not streamed | Responses streamed |
|---|---|---|---|---|
| Vercel AI SDK | text | text | text | text |
| LiteLLM | field | lost | lost | lost |
| LangChain | field | lost | field | field |
| Pydantic AI | field | field | field | field |
field: a refusal field of the result; text: the answer text; lost: nowhere in the result.
Replayed without the structured-output setting of the original request. With that setting supplied, LangChain's streamed Chat Completions cell becomes field. LiteLLM's cells do not change.
What was found
NS is the non-streamed result, S the streamed result of the same response.
Vercel AI SDK (ai 7.0.187, @ai-sdk/anthropic 4.0.113)
| Id | Format | n | Description |
|---|---|---|---|
| AI-1 | Anthropic | 3 | A tool-search result arrives without its call (the call was made in an earlier turn), and the BM25 search tool is declared under its wire name tool_search_tool_bm25 (with the key used in the SDK's documentation, toolSearch, both calls agree). NS: throws “Tool call not found”. S: returns the result. Both label it as the regex search tool. |
| AI-2 | Anthropic | 1 | A citation of type content_block_location has no file_id key (Anthropic's current API reference declares the key required, so current responses most likely include it). NS: throws “Invalid JSON response”. S: returns the text, without citations, and reports a validation error per chunk. |
| AI-3 | Anthropic | 1 | A tool call is cut off by max_tokens. NS: returns the call with empty arguments. S: returns no call. |
LiteLLM 1.111.1
| Id | Format | n | Description |
|---|---|---|---|
| LL-1 | Gemini | 83 | Transport, comment: a comment line makes the stream reader buffer every later event without parsing it. The call returns an empty answer, finish reason stop, and no error. |
| LL-11 | Chat | 4 | The model refuses. NS: refusal text in message.refusal. S: refusal lost, content empty, finish reason stop. |
| LL-3 | Responses | 7 | The answer has url_citation annotations (web search). NS: kept. S: dropped. |
| LL-4 | Gemini | 7 | No thinking-token count is sent. NS: reasoning_tokens is null. S: it is 0 (a difference of representation only). |
| LL-5 | Responses | 6 | The answer has a file_citation or container_file_citation annotation. NS: raises. S: returns the answer. |
| LL-6 | Responses | 4 | The output holds only MCP listing or approval items, or a local shell call. NS: raises “Unknown items”. S: returns an empty answer. |
| LL-7 | Responses | 3 | The output has an apply_patch call. NS: returned as a tool call. S: dropped, and the finish reason is stop. |
| LL-8 | Anthropic | 2 | Thinking is only redacted. NS: reasoning_tokens is null. S: it is 0, although thinking took place. |
| LL-9 | Anthropic | 1 | A tool call's input arrives whole in the block-start event (programmatic tool calling). NS: arguments kept. S: arguments are {}. |
| LL-10 | Gemini | 1 | The response counts tool-use prompt tokens. NS: total_tokens is the provider's total. S: recomputed as prompt plus completion, which leaves them out. |
LangChain (langchain-anthropic 1.7.13, langchain-google-genai 4.5.4, langchain-core 1.8.5)
| Id | Format | n | Description |
|---|---|---|---|
| LC-2 | Anthropic | 28 | The response wrote to the prompt cache. NS: the cache-write count sits under per-TTL keys and cache_creation is 0. S: the same count sits in cache_creation. |
| LC-3 | Anthropic | 18 | The final stream event carries only the output count (older recordings; every Claude 4 or later recording repeats the input count). NS: input tokens reported. S: input_tokens is 0. |
| LC-5 | Gemini | 3 | A text part ends in a newline. NS: trailing newlines removed. S: kept. (A fourth affected recording is counted under LC-6.) |
| LC-6 | Gemini | 2 | A later chunk reports a lower token count than an earlier one. NS: final counts. S: keeps the earlier, larger count. |
| LC-7 | Responses | 1 | A function call is cut off by max_output_tokens. NS: an invalid tool call. S: a valid call with repaired arguments. |
Pydantic AI 2.66.0
| Id | Format | n | Description |
|---|---|---|---|
| PA-3 | Anthropic | 1 | The response contains a server-side model fallback block (a beta feature; the input is a test fixture). NS: raises AssertionError. S: skips the block and returns. |
google-genai 3.6.1, reached through LangChain and Pydantic AI
| Id | Format | n | Description |
|---|---|---|---|
| GG-1 | Gemini | 83 | Transport, comment and nospace: the stream reader accepts only lines that start with data: and a space, so the streamed call raises on every recording. |
If you build on these SDKs
- A feature checked without streaming is not thereby checked with streaming. Refusals, citations, provider-executed tools and token counts are where the modes differed.
- An empty streamed answer with a normal finish reason does not prove the model said nothing.
- Compare usage numbers across both modes before they feed billing.
- A proxy or gateway that adds SSE keep-alive comments in front of Gemini breaks three of the four SDKs on that format, one of them without an error.
Limits
- The non-streamed body is derived from the stream, with the provider's own accumulator, and for Gemini with the study's own merge rule. One derived body pattern (no token usage at all) caused 40 false differences, which are excluded from the results.
- Request settings are replayed for tools, thinking and token limits only. Settings that were not replayed caused 23 false differences. A second review pass caught them, and they are excluded from the results.
- The recordings come from test suites and are not a sample of production traffic, so the rates describe this corpus.
- Results hold for the SDK versions tested in early December 2026. Some causes may already be fixed.
Reproduce
python3.12 -m venv .venv && .venv/bin/pip install -r requirements.txt
(cd js && npm ci)
.venv/bin/python harness/server.py corpus 8787 results/logs/requests.jsonl &
./run_all.sh
.venv/bin/python harness/core_compare.py corpus results
.venv/bin/python harness/usage_conventions.py corpus results
.venv/bin/python harness/analyze.py corpus results paper/tables
(cd paper && tectonic -X compile main.tex)
.venv/bin/python harness/make_site.py
Cite
@misc{ahmad2026streamparity,
title = {Same Response, Two Code Paths: Replay Testing of Streaming Parity in LLM SDKs},
author = {Ahmad, Muhammad},
year = {2026},
note = {Preprint},
doi = {10.5281/zenodo.24734123},
url = {https://doi.org/10.5281/zenodo.24734123}
}