feat(core): resilient structured output (key drift) + activate pipeline gate#117
Merged
Merged
Conversation
…eFormat Structured-output agents on non-OpenAI providers (e.g. Scaleway) fail intermittently with AI_NoObjectGeneratedError when the model returns valid JSON but with drifted keys (casing/separator like documentType↔document_type, or semantic/translated aliases like type↔document_type). Nothing constrains the model since these providers don't enforce responseFormat. Adds two agnostic resilience layers to the structured pipeline, on by default and backward-compatible (a conformant object passes through unchanged, no extra LLM call): 1. Deterministic key normalization — recursively remaps keys onto the schema's property names, insensitive to case/separator. Fixes casing drift with zero LLM cost. 2. Error-driven repair retry — when the object still misses required keys or violates enums, re-queries the model (default 2 attempts) reinjecting the issues and the exact expected keys. Generic safety net for semantic aliases. Also fixes shouldUseStructuredPipeline, which gated on structuredOutput.type but the AI SDK's Output.object() identifies via .name === "object" — so the pipeline (and thus any resilience) was never selected for real usage. Now accepts type ?? name, consistent with getJsonSchemaFromStructuredOutput. New opt-out options on Agent config and per call: normalizeStructuredKeys, structuredOutputRepair. Validated live against Scaleway gpt-oss-120b: drifted keys converge to a zod-conformant object in one repair; disabling resilience reproduces the original failure. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Structured-output agents on non-OpenAI providers (e.g. Scaleway) fail intermittently with
AI_NoObjectGeneratedErrorwhen the model returns valid JSON but with drifted keys — casing/separator (documentType↔document_type) or semantic/translated aliases (type↔document_type). These providers don't enforceresponseFormat, so nothing constrains the model.Fix (agnostic, on by default, backward-compatible)
Two resilience layers in the structured pipeline. A conformant object passes through unchanged with no extra LLM call:
Also fixes
shouldUseStructuredPipeline, which gated onstructuredOutput.typewhile the AI SDK'sOutput.object()identifies via.name === "object"— so the pipeline (and any resilience) was never selected for real usage. Now acceptstype ?? name, consistent withgetJsonSchemaFromStructuredOutput. This activates the structured pipeline for all non-OpenAI structured-output calls (OpenAI unchanged → native structured outputs).New opt-out options (Agent config + per call):
normalizeStructuredKeys,structuredOutputRepair.Validation
tsconfig.build.json: 0 errors. Full suite: 149 passed, 7 skipped, no regressions.gpt-oss-120b, realAgent.generate): drifted{type, summary}→ zod-conformant{document_type, patient_name, short_summary}in one repair; with resilience disabled the drift is not corrected (reproduces the original bug).Bumps
@ai_kit/coreto 1.9.0.🤖 Generated with Claude Code