Skip to content

generateText + Output.object() doesn't use provider-level JSON mode like generateObject #12491

@axelgrubba

Description

@axelgrubba

generateObject (now deprecated) requests JSON mode from the provider (e.g. response_format: { type: "json_object" }), constraining the model to output valid JSON. Its documented replacement generateText + Output.object() does not — it parses free-form text client-side instead.

This causes two issues:

  1. Models that wrap JSON in markdown fences (```json ... ```) fail to parse. extractJsonMiddleware() fixes this.
  2. Even with the middleware, models aren't constrained to produce schema-valid JSON, so validation failures are higher than with generateObject.

Benchmark (20 runs, same prompt, same model):

Approach Success
generateObject 20/20
generateText + Output.object() 17/20
generateText + Output.object() + extractJsonMiddleware() 17/20
import { generateText, Output, extractJsonMiddleware, wrapLanguageModel } from "ai";
import { z } from "zod";

const schema = z.object({
  items: z.array(z.object({ name: z.string(), price: z.number() })).length(6),
});

// Fails ~15% — model wraps JSON in ```json fences or produces invalid output
const result = await generateText({
  model: someOpenAICompatibleModel,
  output: Output.object({ schema }),
  prompt: "Generate 6 products for a store",
});

Expected: Output.object() should request JSON mode from the provider when available, matching generateObject reliability.

AI SDK Version: ai@6.0.81

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

ai/corecore functions like generateText, streamText, etc. Provider utils, and provider spec.ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` label

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions