anthropics / anthropics/claude-agent-sdk-typescript

outputFormat returns subtype 'success' when structured_output is absent — should return error_max_structured_output_retries

Abierto
#277 6 comentarios 3 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Shell
Estrellas
1.8k
Forks
226
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

## Bug: `outputFormat` returns `subtype: "success"` with no `structured_output` for non-trivial schemas

### Environment
- SDK version: `0.2.97`
- Node.js: v22
- OS: Windows 11
- Models tested: `claude-sonnet-4-6`, `claude-haiku-4-5`

### Description

When using `outputFormat` with `query()`, the SDK returns `subtype: "success"` but `structured_output` is `undefined` (key absent from result message) for schemas beyond trivial complexity. The model produces correct JSON in `result` (wrapped in markdown), but the SDK does not populate `structured_output` or return `error_max_structured_output_retries`.

### Reproduction

**Simple schema — WORKS:**
```typescript
for await (const msg of query({
prompt: 'Return JSON with greeting "hello" and number 42.',
options: {
model: "claude-sonnet-4-6",
outputFormat: {
type: "json_schema",
schema: {
type: "object",
properties: {
greeting: { type: "string" },
number: { type: "integer" },
},
required: ["greeting", "number"],
additionalProperties: false,
},
},
maxTurns: 3,
permissionMode: "bypassPermissions",
},
})) {
if (msg.type === "result") {
console.log(msg.subtype); // "success"
console.log(msg.structured_output); // { greeting: "hello", number: 42 } ✅
}
}
```

**Nested schema — FAILS (no `structured_output`):**
```typescript
for await (const msg of query({
prompt: 'Return JSON with version "1", metadata {count:2, label:"test"}, items [{id:"A1", title:"First", status:"active", tags:["a"]}].',
options: {
model: "claude-sonnet-4-6",
outputFormat: {
type: "json_schema",
schema: {
type: "object",
properties: {
version: { type: "string" },
metadata: {
type: "object",
properties: {
count: { type: "integer" },
label: { type: "string" },
},
required: ["count", "label"],
additionalProperties: false,
},
items: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
title: { type: "string" },
status: { type: "string", enum: ["active", "inactive"] },
tags: { type: "array", items: { type: "string" } },
},
required: ["id", "title", "status", "tags"],
additionalProperties: false,
},
},
},
required: ["version", "metadata", "items"],
additionalProperties: false,
},
},
maxTurns: 3,
permissionMode: "bypassPermissions",
},
})) {
if (msg.type === "result") {
console.log(msg.subtype); // "success" (should be error_max_structured_output_retries?)
console.log(msg.structured_output); // undefined ❌
console.log(msg.result); // "Here is the JSON: ```json\n{...}\n```" (correct JSON in text!)
}
}
```

### What we tested

| Schema | `$ref`/`definitions` | `additionalProperties: false` | `structured_output` |
|---|---|---|---|
| Simple (2 fields) | No | Root only | ✅ Present |
| Nested (3 levels, no `$ref`) | No | Every object | ❌ Absent |
| Deep with `$ref` | Yes | Root only | ❌ Absent |
| Inlined (resolved `$ref`) | No | Every object | ❌ Absent |

All failing cases return `subtype: "success"` — never `error_max_structured_output_retries`.

### Expected behavior

1. If the schema is supported: `structured_output` should be populated with validated JSON
2. If the schema is too complex: `subtype` should be `error_max_structured_output_retries`, not `success`
3. The SDK should never return `subtype: "success"` with `structured_output: undefined` when `outputFormat` is configured

### Notes

- The raw Messages API (`tool_use` + `tool_choice`) correctly handles these schemas — the API enforces nested types including enums and `$ref`. This is an SDK-specific issue.
- The model consistently produces correct JSON in `result` text (wrapped in markdown), showing it understands the schema — the SDK just doesn't extract/validate it.
- Per the [structured outputs docs](https://code.claude.com/docs/en/agent-sdk/structured-outputs), `outputFormat` should work with multi-turn tool use: "The agent can use any tools it needs to complete the task, and you still get validated JSON matching your schema at the end."

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.