Skip to content

Commit 73d9943

Browse files
committed
fix(examples): export TanStack AI traces when the model call fails
The route called chat({ stream: false }). At @tanstack/ai 0.64.0 that collects text with streamToText, which throws at the first RUN_ERROR and stops reading (stream-to-response.ts). The engine's finally only fires onAbort when the run was cancelled, so onError never ran and otelMiddleware never ended the chat or model-call span. A model HTTP 500 failed the request and exported nothing. answer() now reads chat()'s stream to the end, keeps the first RUN_ERROR, and throws its message after the stream ends. The CLI prints "chat failed: <message>" and exits 1, so the caller still gets an error response. Test first: the fake OpenAI server gains fail_status=500. The new contract test failed with no export at all, and now sees the root and model-call spans exported with status ERROR and message "500 boom". approved: Nikhil 2026-10-03 blanket Refs: TH-8238
1 parent 04409aa commit 73d9943

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

‎typescript/examples/tanstack-ai/src/chat.mjs‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,37 @@ const getWeather = toolDefinition({
2323
},
2424
}).server(async ({ city }) => ({ city, forecast: "sunny", celsius: 21 }));
2525

26-
/** Answer one question. The adapter reads OPENAI_API_KEY and OPENAI_BASE_URL. */
26+
/**
27+
* Answer one question. The adapter reads OPENAI_API_KEY and OPENAI_BASE_URL.
28+
*
29+
* Reads chat()'s stream to the end, even after a RUN_ERROR chunk, and only
30+
* then throws. Do not use `stream: false` with otelMiddleware: at
31+
* @tanstack/ai 0.64.0 it collects text with streamToText, which throws at
32+
* the first RUN_ERROR and stops reading. The run then never reaches its
33+
* onError hook, so the chat and model-call spans are never ended or exported
34+
* and a failed request leaves no trace.
35+
*/
2736
export async function answer(question, middleware) {
28-
return chat({
37+
const stream = chat({
2938
adapter: openaiChatCompletions(process.env.OPENAI_MODEL ?? "gpt-4o-mini"),
3039
systemPrompts: ["You are a concise weather assistant."],
3140
messages: [{ role: "user", content: question }],
3241
tools: [getWeather],
3342
middleware: [middleware],
34-
stream: false,
3543
});
44+
let text = "";
45+
let runError = null;
46+
for await (const chunk of stream) {
47+
if (chunk.type === "RUN_ERROR") {
48+
runError ??= chunk;
49+
} else if (chunk.type === "TEXT_MESSAGE_CONTENT" && chunk.delta) {
50+
text += chunk.delta;
51+
}
52+
}
53+
if (runError) {
54+
throw new Error(runError.message || runError.error?.message || "chat failed");
55+
}
56+
return text;
3657
}
3758

3859
/** The route: one traced chat() call, flushed in finally. */
@@ -52,6 +73,10 @@ async function main() {
5273
const tracerProvider = registerFutureAgiTracing();
5374
try {
5475
console.log(await chatRoute(question, tracerProvider));
76+
} catch (error) {
77+
// The route's error response. The spans are already exported.
78+
console.error(`chat failed: ${error?.message ?? String(error)}`);
79+
process.exitCode = 1;
5580
} finally {
5681
await shutdownTraces(tracerProvider);
5782
}

‎typescript/examples/tanstack-ai/tests/_fake_openai.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
call. Any other request gets a text answer. Both responses end with an OpenAI
55
include_usage chunk, so the adapter reports token usage. With ``stall=True``
66
the text answer stops after its first delta until the server exits, so a
7-
client can abort mid-stream.
7+
client can abort mid-stream. With ``fail_status=500`` every request gets that
8+
HTTP status and an OpenAI-shaped error body whose message is ``boom``.
89
"""
910

1011
from __future__ import annotations
@@ -48,8 +49,10 @@ def _chunk(delta: dict[str, Any] | None, finish_reason: str | None = None,
4849
class FakeOpenAI:
4950
"""Serve /v1/chat/completions on 127.0.0.1 and record each request body."""
5051

51-
def __init__(self, tool_city: str, answer_text: str, stall: bool = False) -> None:
52+
def __init__(self, tool_city: str, answer_text: str, stall: bool = False,
53+
fail_status: int | None = None) -> None:
5254
self.requests: list[dict[str, Any]] = []
55+
self.authorizations: list[str | None] = []
5356
self._release = threading.Event()
5457
lock = threading.Lock()
5558
owner = self
@@ -63,6 +66,18 @@ def do_POST(self) -> None: # noqa: N802
6366
request = json.loads(self.rfile.read(length).decode("utf-8"))
6467
with lock:
6568
owner.requests.append(request)
69+
owner.authorizations.append(self.headers.get("Authorization"))
70+
71+
if fail_status is not None:
72+
body = json.dumps(
73+
{"error": {"message": "boom", "type": "server_error"}}
74+
).encode("utf-8")
75+
self.send_response(fail_status)
76+
self.send_header("Content-Type", "application/json")
77+
self.send_header("Content-Length", str(len(body)))
78+
self.end_headers()
79+
self.wfile.write(body)
80+
return
6681

6782
has_tool_result = any(
6883
message.get("role") == "tool" for message in request.get("messages", [])

‎typescript/examples/tanstack-ai/tests/test_tanstack_ai_contract.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,37 @@ def test_unreachable_collector_does_not_fail_chat():
271271
assert "[futureagi] span export failed" in result.stderr.decode()
272272

273273

274+
def test_failed_model_call_exports_error_spans():
275+
"""A model call that fails still ends and exports the root and LLM spans.
276+
277+
chat({ stream: false }) stops reading at RUN_ERROR, the run never reaches
278+
onError, and otelMiddleware never ends its spans. The route drains the
279+
stream instead, then fails the request.
280+
"""
281+
with Receiver() as receiver, FakeOpenAI(CITY, ANSWER, fail_status=500) as fake:
282+
result = _node(EXAMPLE / "src" / "chat.mjs", receiver.origin, fake)
283+
spans = _by_name(receiver.spans())
284+
exports = receiver.requests()
285+
model_requests = list(fake.requests)
286+
287+
# The caller still gets an error, not an answer.
288+
assert result.returncode != 0, result.stdout.decode()
289+
assert "500 boom" in result.stderr.decode()
290+
assert ANSWER not in result.stdout.decode()
291+
assert model_requests
292+
293+
# And the failure is in Future AGI.
294+
assert exports
295+
assert all(export["path"] == "/tracer/v1/traces" for export in exports)
296+
assert sorted(spans) == sorted([ROOT, ITERATION_0])
297+
for name in (ROOT, ITERATION_0):
298+
status = spans[name].get("status", {})
299+
assert _is_error(status), (name, status)
300+
assert status.get("message") == "500 boom", (name, status)
301+
assert spans[ITERATION_0]["parentSpanId"] == spans[ROOT]["spanId"]
302+
assert _attributes(spans[ITERATION_0])["gen_ai.span.kind"] == "LLM"
303+
304+
274305
def test_abort_mid_stream_ends_spans_as_cancelled():
275306
with Receiver() as receiver, FakeOpenAI(
276307
CITY, ANSWER, stall=True

0 commit comments

Comments
 (0)