Skip to content

Commit 3824708

Browse files
committed
feat(agency): robust structured output with brace-matched JSON extraction and validation retries
parseStructuredOutput now: - Tries direct JSON parse (happy path) - Strips markdown code fences (```json ... ```) - Falls back to brace-matched extraction that walks the string respecting quote and escape state, so nested braces in string values and trailing commentary after a valid JSON object no longer break parsing Agency execute() now retries validation failures: - New controls.maxValidationRetries field (default 1) - When opts.output is set and parsed is undefined, retry the generation with an error-feedback hint appended to the prompt so the model can self-correct Fixes the class of failures where providers return valid JSON followed by commentary.
1 parent 067a554 commit 3824708

60 files changed

Lines changed: 1538 additions & 90 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dist/api/AgentOS.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/AgentOS.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/agency.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/agency.js

Lines changed: 98 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/agency.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/api/agent.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ export interface AgentOptions extends BaseAgentConfig {
5555
onFallback?: (error: Error, fallbackProvider: string) => void;
5656
/** Model router for intelligent provider selection per-call. */
5757
router?: IModelRouter;
58+
/**
59+
* Optional Zod schema for validating the LLM's structured output.
60+
*
61+
* When provided, the agent's `generate()` result includes a `parsed` field
62+
* with the Zod-validated and typed output. JSON extraction and validation
63+
* happen automatically in the `onAfterGeneration` hook. On validation failure,
64+
* the agent retries internally (up to `controls.maxValidationRetries ?? 1`).
65+
*
66+
* When omitted, behavior is unchanged — `result.parsed` is undefined.
67+
* This is a non-breaking additive change.
68+
*
69+
* @example
70+
* ```ts
71+
* import { z } from 'zod';
72+
* const myAgent = agent({
73+
* name: 'Extractor',
74+
* instructions: 'Extract entities as JSON',
75+
* responseSchema: z.object({ entities: z.array(z.string()) }),
76+
* });
77+
* const result = await myAgent.generate('Find entities in: ...');
78+
* console.log(result.parsed?.entities); // string[]
79+
* ```
80+
*/
81+
responseSchema?: import('zod').ZodType;
5882
/** Pre-generation hook, called before each LLM step. */
5983
onBeforeGeneration?: (context: GenerationHookContext) => Promise<GenerationHookContext | void>;
6084
/** Post-generation hook, called after each LLM step. */

dist/api/agent.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)