Skip to content

refactor(py/anthropic)!: flat AnthropicConfig with exported Literals - #6700

Merged
huangjeff5 merged 1 commit into
fix/py-plugin-tool-choicefrom
refactor/py-anthropic-flat-config
Oct 11, 2026
Merged

huangjeff5 merged 1 commit into
fix/py-plugin-tool-choicefrom
refactor/py-anthropic-flat-config

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Stack: 4/4 · previous #6699

AnthropicConfig is flat. Choices are exported Literals, and the plugin builds Anthropic's nested thinking, output_config and metadata objects. For equivalent configs, request bodies and anthropic-beta headers are byte-identical to main.

Previously, AnthropicConfig nested four SDK-shaped classes, ThinkingConfig, OutputConfig, TaskBudget and RequestMetadata, with three spellings of the thinking mode (enabled, adaptive, type). Some combinations were silently dropped: a budget with thinking disabled, or a display with no mode.

from genkit_anthropic import AnthropicConfig

# --- BEFORE ---
AnthropicConfig(
    thinking=ThinkingConfig(type='enabled', budget_tokens=2048),
    output_config=OutputConfig(effort='high', task_budget=TaskBudget(total=20000)),
)

# --- AFTER ---
# 1. Build a flat Anthropic config
config = AnthropicConfig(thinking='enabled', thinking_budget=2048, effort='high', task_budget=20000)

# 2. The plugin builds the Messages API body
# => {'thinking': {'type': 'enabled', 'budget_tokens': 2048},
#     'output_config': {'effort': 'high', 'task_budget': {'type': 'tokens', 'total': 20000}}, ...}

# 3. The old nested shape names its replacement
AnthropicConfig.model_validate({'output_config': {'effort': 'high'}})
# => GenkitError: INVALID_ARGUMENT: anthropic: output_config was flattened; use effort= and task_budget=

Decisions

  • Provider configs are designed Genkit surfaces: flat fields and exported Literals where the provider nests objects; the plugin builds the provider's wire shape. The fields are thinking (ThinkingMode), thinking_budget, thinking_display (ThinkingDisplay), effort (Effort), task_budget and user_id. They map to thinking, output_config (merged with the structured-output format) and metadata.user_id. thinking= is the one spelling of the mode.
  • Ambiguous thinking settings raise instead of being dropped: a budget alone means enabled, and 'enabled' requires one. 'adaptive' accepts and ignores a budget, as before. 'disabled' with a budget, and thinking_display with no mode, now raise; main silently dropped both.
  • extra still replaces a built object, except output_config: extra={'thinking': {...}} or extra={'metadata': {...}} goes out whole. output_config stays a managed field, because replacing it would drop the structured-output format Genkit merges in.

Breaking changes

  • Removed ThinkingConfig, OutputConfig, TaskBudget and RequestMetadata. thinking={...}, output_config= and metadata= raise INVALID_ARGUMENT naming the flat field. The enabled / adaptive bools are gone.
  • thinking='disabled' with a budget, and thinking_display with no mode, now raise.
  • thinking_budget must be an integer; 'adaptive' used to accept a fractional budget.
  • Dev UI schema: the thinking, output_config and metadata objects are replaced by thinking (enum), thinkingBudget, thinkingDisplay, effort, taskBudget and userId.

Verification

  • ruff format and check, pyrefly, pyright packages/*/src, ty, generate_schema_typing --ci, uv lock --check: all clean. pyright, pyrefly and ty on packages/*/tests/config_typing_test.py: 0 errors.
  • pytest: 5275 passed, 45 skipped.
  • Wire: intercepted the real HTTP requests for 11 cases, old nested config on main vs flat config here: thinking enabled, budget only, adaptive, disabled, display with enabled and adaptive, effort, task_budget, effort + task_budget + structured output, user_id, and all of them together. URLs, anthropic-beta headers and bodies are byte-identical; task_budget still selects the beta API with the default betas.
  • Vertex Model Garden Claude uses AnthropicConfig, so it gets the same surface; its tests pass.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request flattens the AnthropicConfig structure in the genkit-anthropic package, replacing previously nested configuration models (such as ThinkingConfig, OutputConfig, and RequestMetadata) with flat, top-level fields including thinking_budget, thinking_display, effort, task_budget, and user_id. The plugin now internally constructs the nested request objects for the Anthropic API from these flat fields. Additionally, migration validators have been introduced to provide clear error messages for deprecated nested keys, and all tests, samples, and documentation have been updated accordingly. I have no feedback to provide as there are no review comments.

@huangjeff5
huangjeff5 requested a review from hilariie October 9, 2026 15:46
@huangjeff5

Copy link
Copy Markdown
Contributor Author

Does "thinking" need to be exposed? Can it be inferred from the presence of thinking_budget?

AnthropicConfig no longer nests Anthropic's request objects. The plugin
builds them from flat fields:

- thinking (ThinkingMode: 'enabled' | 'adaptive' | 'disabled'),
  thinking_budget and thinking_display (ThinkingDisplay) -> thinking
- effort (Effort: 'low' ... 'max') and task_budget -> output_config,
  sent as {'effort': ..., 'task_budget': {'type': 'tokens', 'total': N}}
  and merged with the structured-output format
- user_id -> metadata.user_id

Remove ThinkingConfig, OutputConfig, TaskBudget and RequestMetadata, and
the legacy enabled/adaptive bools. Export Effort, ThinkingMode and
ThinkingDisplay.

Rules: a thinking_budget alone means enabled; 'enabled' requires one;
'disabled' now rejects one (it was silently dropped); 'adaptive' accepts
and ignores it, as before. thinking_display with no mode now raises (it
was silently dropped).

The old nested keys (thinking={...}, output_config, metadata) raise
INVALID_ARGUMENT naming the flat replacement. extra={'thinking': ...}
and extra={'metadata': ...} still replace the built object;
output_config stays managed because it carries the structured-output
format.

Request bodies and anthropic-beta headers are byte-identical to main for
equivalent configs. task_budget still selects the beta API.
@huangjeff5
huangjeff5 force-pushed the refactor/py-anthropic-flat-config branch from 65ddad6 to 3dca646 Compare October 11, 2026 00:21
@huangjeff5
huangjeff5 merged commit 3dca646 into main Oct 11, 2026
7 checks passed
@huangjeff5
huangjeff5 deleted the refactor/py-anthropic-flat-config branch October 11, 2026 00:55
@huangjeff5

Copy link
Copy Markdown
Contributor Author

Folded into #6699: both PRs rewrite the same AnthropicConfig code, so one review covers the whole Anthropic surface. GitHub shows this as merged because its commit now sits on #6699's branch (this PR's base); nothing reached main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation python Python refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant