MCPcopy Index your code
hub / github.com/TanStack/ai / runStreamingStructuredOutputImpl

Function runStreamingStructuredOutputImpl

packages/ai/src/activities/chat/index.ts:3136–3207  ·  view source on GitHub ↗
(
  options: TextActivityOptions<AnyTextAdapter, TSchema, true, TContext>,
  jsonSchema: NonNullable<ReturnType<typeof convertSchemaToJsonSchema>>,
  normalize: (data: unknown) => unknown,
)

Source from the content-addressed store, hash-verified

3134>
3135
3136async function* runStreamingStructuredOutputImpl<
3137 TSchema extends SchemaInput,
3138 TContext = unknown,
3139>(
3140 options: TextActivityOptions<AnyTextAdapter, TSchema, true, TContext>,
3141 jsonSchema: NonNullable<ReturnType<typeof convertSchemaToJsonSchema>>,
3142 normalize: (data: unknown) => unknown,
3143): StructuredOutputStreamInternal<InferSchemaType<TSchema>> {
3144 const {
3145 adapter,
3146 outputSchema,
3147 middleware,
3148 context,
3149 debug,
3150 mcp,
3151 ...textOptions
3152 } = options
3153 const model = adapter.model
3154 const logger = resolveDebugOption(debug)
3155
3156 // Per issue #605: adapters that natively combine tools + schema-constrained
3157 // output in one streaming call (modern OpenAI, Anthropic 4.5+, Gemini 3+,
3158 // Grok 4+) opt in via `supportsCombinedToolsAndSchema()`. The engine then
3159 // forwards the schema into the regular `chatStream` call and harvests the
3160 // structured result from the agent loop's accumulated text — no separate
3161 // finalization round-trip, and the `'structuredOutput'` middleware phase
3162 // does not fire.
3163 const nativeCombined =
3164 adapter.supportsCombinedToolsAndSchema?.(options.modelOptions) === true
3165
3166 const mcpManager = MCPManager.from(mcp)
3167 const mcpTools = await mcpManager.discover()
3168 if (mcpTools.length > 0) {
3169 textOptions.tools = [...(textOptions.tools ?? []), ...mcpTools]
3170 }
3171
3172 // Inputs may be UIMessages (from useChat) or ModelMessages (from server-side
3173 // callers). TextEngine handles the conversion uniformly.
3174 const engine = new TextEngine(
3175 {
3176 adapter,
3177 params: { ...textOptions, model, logger } as TextOptions<
3178 Record<string, unknown>,
3179 Record<string, unknown>,
3180 TContext
3181 >,
3182 middleware,
3183 context,
3184 finalStructuredOutput: {
3185 jsonSchema,
3186 yieldChunks: true,
3187 normalize,
3188 ...(nativeCombined ? { nativeCombined: true } : {}),
3189 },
3190 },
3191 logger,
3192 )
3193

Callers 1

Calls 6

runMethod · 0.95
resolveDebugOptionFunction · 0.90
fromMethod · 0.80
discoverMethod · 0.80
disposeMethod · 0.45

Tested by

no test coverage detected