Skip to content

fix(vertexai): recognize "adaptive" thinking in _thinking_in_params#1820

Open
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/vertexai-thinking-in-params-adaptive
Open

fix(vertexai): recognize "adaptive" thinking in _thinking_in_params#1820
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/vertexai-thinking-in-params-adaptive

Conversation

@HumphreySun98

Copy link
Copy Markdown

Description

_thinking_in_params in libs/vertexai/langchain_google_vertexai/_anthropic_utils.py only matched thinking.type == "enabled". When callers set thinking.type = "adaptive" — Anthropic's recommended thinking mode for Claude 4.6+, and the only mode supported by Claude Opus 4.7 — the helper returns False. In model_garden.py (and the streaming counterpart at L520), that flips coerce_content_to_string to True for both _generate and _stream, so the response's structured thinking blocks get silently flattened to a plain string. Net effect: ChatAnthropicVertex cannot return thinking blocks for Claude Opus 4.7 at all.

The fix is to match "adaptive" alongside "enabled":

def _thinking_in_params(params: dict) -> bool:
    return params.get("thinking", {}).get("type") in ("enabled", "adaptive")

Relevant issues

Fixes #1711

Type

🐛 Bug Fix

Changes

  • libs/vertexai/langchain_google_vertexai/_anthropic_utils.py: accept both "enabled" and "adaptive".
  • libs/vertexai/tests/unit_tests/test_anthropic_utils.py: add test_thinking_in_params_adaptive next to the existing enabled / disabled cases.

Testing

$ uv run pytest libs/vertexai/tests/unit_tests/test_anthropic_utils.py -k thinking_in_params -v
test_thinking_in_params_true                  PASSED
test_thinking_in_params_false_different_type  PASSED
test_thinking_in_params_adaptive              PASSED
3 passed in 11.90s

Reverting only the one-line _anthropic_utils.py change while keeping the new test makes that test fail with AssertionError, confirming the regression coverage pins the buggy behavior.

ruff check and ruff format --check pass on both files.

Note

The fix is intentionally minimal — a tuple-membership check that mirrors what the issue reporter proposed. No behavior changes for callers using "enabled" or any other value.

Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.

`_thinking_in_params` in `_anthropic_utils.py` only matched
`thinking.type == "enabled"`, so when callers used `thinking.type =
"adaptive"` (Anthropic's recommended thinking mode for Claude 4.6+ and
the only mode supported by Claude Opus 4.7) the helper returned `False`.

In `model_garden.py` that wired `coerce_content_to_string = True` for
both `_generate` and `_stream`, and the response's structured thinking
blocks were silently flattened to a plain string.

Match `"adaptive"` alongside `"enabled"` so adaptive-thinking responses
keep their thinking blocks intact. Adds a regression unit test for the
adaptive case next to the existing `enabled` / `disabled` ones.

Fixes langchain-ai#1711
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.

ChatAnthropicVertex: _thinking_in_params does not recognize thinking.type = "adaptive", causing thinking blocks to be stripped

1 participant