Skip to content

Conversation

@dhofheinz
Copy link

Summary

Adds a comprehensive example demonstrating how to produce structured JSON output using the tool_choice parameter. This addresses the documentation request in #816.

What This PR Does

  • Adds examples/structured-output.ts with two complete examples:
    1. Non-streaming version: Shows how to force structured output and extract the result
    2. Streaming version: Demonstrates progressive structured output with delta handling

Key Feature Demonstrated

The critical pattern that enables structured output:

tool_choice: { type: 'tool', name: 'json' }

By defining a tool with the desired JSON schema and forcing its use via tool_choice, Claude returns guaranteed structured output matching the schema.

Examples Included

  1. Vegetarian lasagna recipe - Generates ingredients list as structured JSON array
  2. Paris landmarks - Demonstrates streaming structured output with location data

Additional Changes

The repository's formatter made minor whitespace improvements to:

  • .github/workflows/create-releases.yml
  • .github/workflows/release-docker.yml
  • bin/cli

These are cosmetic-only changes that improve consistency.

Documentation

The example includes:

  • Clear inline comments explaining the pattern
  • Practical use cases
  • Both streaming and non-streaming approaches
  • Type-safe tool use block extraction

Fixes #816


Note: This example demonstrates the workaround pattern mentioned in the issue, as the Anthropic API currently doesn't provide a first-class structured output feature. The tool_choice approach is the recommended method per the issue discussion.

Demonstrates how to use tool_choice to force structured JSON output
matching a schema. Includes both streaming and non-streaming variants.

Fixes anthropics#816
@dhofheinz dhofheinz requested a review from a team as a code owner October 7, 2025 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document how to produce structured output

1 participant