MCPcopy
hub / github.com/TanStack/ai / structuredOutputStream

Function structuredOutputStream

packages/openai-base/src/adapters/responses-text.ts:282–646  ·  view source on GitHub ↗

* Stream structured output via the Responses API: single request with * `text.format: json_schema` + `stream: true`. Consumes Responses-API * events (`response.output_text.delta`, `response.reasoning_text.delta`, * `response.reasoning_summary_text.delta`, `response.refusal.delta`, * `res

(
    options: StructuredOutputOptions<TProviderOptions>,
  )

Source from the content-addressed store, hash-verified

280 * CUSTOM event's `value.reasoning`.
281 */
282 async *structuredOutputStream(
283 options: StructuredOutputOptions<TProviderOptions>,
284 ): AsyncIterable<StreamChunk> {
285 const { chatOptions, outputSchema } = options
286 const requestParams = this.mapOptionsToRequest(chatOptions)
287
288 const jsonSchema = this.makeStructuredOutputCompatible(
289 outputSchema,
290 outputSchema.required,
291 )
292
293 const timestamp = Date.now()
294 const aguiState = {
295 runId: generateId(this.name),
296 threadId: chatOptions.threadId ?? generateId(this.name),
297 messageId: generateId(this.name),
298 timestamp,
299 hasEmittedRunStarted: false,
300 }
301
302 let accumulatedContent = ''
303 let accumulatedReasoning = ''
304 let hasEmittedTextMessageStart = false
305 let reasoningMessageId: string | undefined
306 let stepId: string | undefined
307 let hasClosedReasoning = false
308 let model: string = chatOptions.model
309 let usage: OpenAI.Responses.Response['usage'] | undefined
310
311 const closeReasoning = function* (this: {
312 name: string
313 }): Generator<StreamChunk> {
314 if (reasoningMessageId && !hasClosedReasoning) {
315 hasClosedReasoning = true
316 yield {
317 type: EventType.REASONING_MESSAGE_END,
318 messageId: reasoningMessageId,
319 model,
320 timestamp,
321 }
322 yield {
323 type: EventType.REASONING_END,
324 messageId: reasoningMessageId,
325 model,
326 timestamp,
327 }
328 if (stepId) {
329 yield {
330 type: EventType.STEP_FINISHED,
331 stepName: stepId,
332 stepId,
333 model,
334 timestamp,
335 content: accumulatedReasoning,
336 }
337 }
338 }
339 }.bind(this)

Callers

nothing calls this directly

Calls 15

generateIdFunction · 0.90
extractRequestOptionsFunction · 0.90
buildResponsesUsageFunction · 0.90
toRunErrorPayloadFunction · 0.85
toRunErrorRawEventFunction · 0.85
nowMethod · 0.80
providerMethod · 0.80
parseMethod · 0.80
errorsMethod · 0.80
createMethod · 0.65
closeReasoningFunction · 0.50
mapOptionsToRequestMethod · 0.45

Tested by

no test coverage detected