Skip to content
Source
Sign inSign up
TanStack/ai

feat(ai): structured-output middleware coverage (closes #390) #600

MergedAlemTuzlakmerged 20 commits intomain fromrefs/pull/600/headmerged by AlemTuzlak View on GitHub

Showing 39 changed files with 97 additions and 4 deletions (loading 36).

x
0 of 39 viewed, 39 left
21 additions and 0 deletions.changeset/structured-output-middleware-coverage.mdAdded
View file
@@ -0,0 +1,21 @@
+---
+'@tanstack/ai': minor
+---
+​
+Middleware now wraps the final structured-output provider call in `chat({ outputSchema })` (both Promise<T> and streaming variants). Closes #390.
+​
+**New public surface:**
+​
+- `ChatMiddlewarePhase` gains a `'structuredOutput'` value, set on `ChatMiddlewareContext` for the duration of the final structured-output adapter call.
+- New optional `ChatMiddleware.onStructuredOutputConfig` hook receives a `StructuredOutputMiddlewareConfig` (including the JSON Schema being sent to the provider) and can return a partial to transform the config before the final call.
+- New exported type `StructuredOutputMiddlewareConfig` extends `ChatMiddlewareConfig` with `outputSchema: JSONSchema`.
+​
+**Behavior change for existing middleware:**
+​
+- `onChunk` now observes chunks from the final structured-output call. Phase-aware middleware can branch on `ctx.phase === 'structuredOutput'` to opt out: `if (ctx.phase === 'structuredOutput') return`.
+- `onFinish` fires once at the end of the whole `chat()` invocation, after finalization completes — not after the agent loop.
+- `onFinish.info` reflects the agent loop's terminal state only. `info.usage` / `info.finishReason` / `info.content` do not include the final structured-output adapter call. To observe finalization tokens, use `onUsage` (fires for both agent-loop and finalization `RUN_FINISHED` events).
+​
+**Internal cleanup:**
+​
+- The previous `RUN_STARTED`/`RUN_FINISHED` suppression hack in `runStreamingStructuredOutput` was relocated into the engine (`streamModelResponse`) and gated on `finalStructuredOutput.yieldChunks`. The streaming consumer still sees exactly one outer pair around the whole run.
74 additions and 2 deletionsdocs/advanced/middleware.mdModified
View file
@@ -71,13 +71,21 @@