feat: support claude agent SDK-style structured outputs in the OpenCode SDK (#8161)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Dax Raad <d@ironbay.co>
This commit is contained in:
Kyle Mistele
2026-02-11 20:54:05 -08:00
committed by GitHub
parent 66780195dc
commit e269788a8f
10 changed files with 854 additions and 66 deletions

View File

@@ -50,6 +50,16 @@ import { Truncate } from "@/tool/truncation"
// @ts-ignore
globalThis.AI_SDK_LOG_WARNINGS = false
const STRUCTURED_OUTPUT_DESCRIPTION = `Use this tool to return your final response in the requested structured format.
IMPORTANT:
- You MUST call this tool exactly once at the end of your response
- The input must be valid JSON matching the required schema
- Complete all necessary research and tool calls BEFORE calling this tool
- This tool provides your final answer - no further actions are taken after calling it`
const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.`
export namespace SessionPrompt {
const log = Log.create({ service: "session.prompt" })
@@ -96,6 +106,7 @@ export namespace SessionPrompt {
.describe(
"@deprecated tools and permissions have been merged, you can set permissions on the session itself now",
),
format: MessageV2.Format.optional(),
system: z.string().optional(),
variant: z.string().optional(),
parts: z.array(
@@ -276,6 +287,11 @@ export namespace SessionPrompt {
using _ = defer(() => cancel(sessionID))
// Structured output state
// Note: On session resumption, state is reset but outputFormat is preserved
// on the user message and will be retrieved from lastUser below
let structuredOutput: unknown | undefined
let step = 0
const session = await Session.get(sessionID)
while (true) {
@@ -589,6 +605,16 @@ export namespace SessionPrompt {
messages: msgs,
})
// Inject StructuredOutput tool if JSON schema mode enabled
if (lastUser.format?.type === "json_schema") {
tools["StructuredOutput"] = createStructuredOutputTool({
schema: lastUser.format.schema,
onSuccess(output) {
structuredOutput = output
},
})
}
if (step === 1) {
SessionSummary.summarize({
sessionID: sessionID,
@@ -619,12 +645,19 @@ export namespace SessionPrompt {
await Plugin.trigger("experimental.chat.messages.transform", {}, { messages: sessionMessages })
// Build system prompt, adding structured output instruction if needed
const system = [...(await SystemPrompt.environment(model)), ...(await InstructionPrompt.system())]
const format = lastUser.format ?? { type: "text" }
if (format.type === "json_schema") {
system.push(STRUCTURED_OUTPUT_SYSTEM_PROMPT)
}
const result = await processor.process({
user: lastUser,
agent,
abort,
sessionID,
system: [...(await SystemPrompt.environment(model)), ...(await InstructionPrompt.system())],
system,
messages: [
...MessageV2.toModelMessages(sessionMessages, model),
...(isLastStep
@@ -638,7 +671,33 @@ export namespace SessionPrompt {
],
tools,
model,
toolChoice: format.type === "json_schema" ? "required" : undefined,
})
// If structured output was captured, save it and exit immediately
// This takes priority because the StructuredOutput tool was called successfully
if (structuredOutput !== undefined) {
processor.message.structured = structuredOutput
processor.message.finish = processor.message.finish ?? "stop"
await Session.updateMessage(processor.message)
break
}
// Check if model finished (finish reason is not "tool-calls" or "unknown")
const modelFinished = processor.message.finish && !["tool-calls", "unknown"].includes(processor.message.finish)
if (modelFinished && !processor.message.error) {
if (format.type === "json_schema") {
// Model stopped without calling StructuredOutput tool
processor.message.error = new MessageV2.StructuredOutputError({
message: "Model did not produce structured output",
retries: 0,
}).toObject()
await Session.updateMessage(processor.message)
break
}
}
if (result === "stop") break
if (result === "compact") {
await SessionCompaction.create({
@@ -669,7 +728,8 @@ export namespace SessionPrompt {
return Provider.defaultModel()
}
async function resolveTools(input: {
/** @internal Exported for testing */
export async function resolveTools(input: {
agent: Agent.Info
model: Provider.Model
session: Session.Info
@@ -849,6 +909,36 @@ export namespace SessionPrompt {
return tools
}
/** @internal Exported for testing */
export function createStructuredOutputTool(input: {
schema: Record<string, any>
onSuccess: (output: unknown) => void
}): AITool {
// Remove $schema property if present (not needed for tool input)
const { $schema, ...toolSchema } = input.schema
return tool({
id: "StructuredOutput" as any,
description: STRUCTURED_OUTPUT_DESCRIPTION,
inputSchema: jsonSchema(toolSchema as any),
async execute(args) {
// AI SDK validates args against inputSchema before calling execute()
input.onSuccess(args)
return {
output: "Structured output captured successfully.",
title: "Structured Output",
metadata: { valid: true },
}
},
toModelOutput(result) {
return {
type: "text",
value: result.output,
}
},
})
}
async function createUserMessage(input: PromptInput) {
const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent()))
@@ -870,6 +960,7 @@ export namespace SessionPrompt {
agent: agent.name,
model,
system: input.system,
format: input.format,
variant,
}
using _ = defer(() => InstructionPrompt.clear(info.id))