Skip to content

Add anthropic-beta header for structured outputs.#163

Merged
felixarntz merged 1 commit intoWordPress:trunkfrom
johnhooks:fix/issue-151-2
Jan 8, 2026
Merged

Add anthropic-beta header for structured outputs.#163
felixarntz merged 1 commit intoWordPress:trunkfrom
johnhooks:fix/issue-151-2

Conversation

@johnhooks
Copy link
Contributor

When requesting structured JSON output from the Anthropic API using the output_format parameter with a JSON schema, the API requires the anthropic-beta: structured-outputs-2025-11-13 header to be present in the request. Without this header, Anthropic rejects the request because structured outputs is currently a beta feature that must be explicitly opted into. This fix detects when JSON schema output is configured via getOutputMimeType() and getOutputSchema() and automatically adds the required beta header, allowing structured output requests to succeed.

See Claude Docs: Structured outputs

Fixes #151.

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: johnhooks <bitmachina@git.wordpress.org>
Co-authored-by: JasonTheAdams <jason_the_adams@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@johnhooks
Copy link
Contributor Author

@JasonTheAdams I was going to add a test or two, but at the moment there aren't any tests for AnthropicTextGenerationModel, or am I missing something?

@JasonTheAdams
Copy link
Member

Thanks, @johnhooks! You're right. There aren't currently any tests. I'll let @felixarntz decide if he'd like to add tests in this PR since he built out #156. I'm also adding him as a reviewer.

@johnhooks
Copy link
Contributor Author

johnhooks commented Jan 5, 2026

I had tests in my implementation, before #156 was created AnthropicTextGenerationModelTest.php. I could attempt to port it over and add tests for all of AnthropicTextGenerationModel.

UPDATE: I updated the test for the current implementation, and intended to stack the PRs, but I don't think I can since the incoming branch is from my fork. But I can either do separate PR, or merge into this one. https://github.com/johnhooks/php-ai-client/blob/cbaf7dc4f634bc7e6bd8676a393d600eb05034bc/tests/unit/ProviderImplementations/Anthropic/AnthropicTextGenerationModelTest.php

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johnhooks I think this is good to go. While we should probably have tests for these, I think this is its own task to do, and this change is simple enough to not require being blocked to add tests.

Great catch, thank you for fixing this!

@felixarntz felixarntz added the [Type] Bug An existing feature does not function as intended label Jan 8, 2026
@felixarntz felixarntz added this to the 0.4.0 milestone Jan 8, 2026
@felixarntz felixarntz merged commit 5fcd526 into WordPress:trunk Jan 8, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic Structured Output Issue

3 participants