From 0786b3bd83b19805752abbb98d9e93d75ed90c38 Mon Sep 17 00:00:00 2001 From: Humphrey Sun Date: Mon, 1 Jun 2026 17:35:13 -0500 Subject: [PATCH] fix(vertexai): recognize "adaptive" thinking in `_thinking_in_params` `_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 #1711 --- .../langchain_google_vertexai/_anthropic_utils.py | 2 +- .../vertexai/tests/unit_tests/test_anthropic_utils.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/vertexai/langchain_google_vertexai/_anthropic_utils.py b/libs/vertexai/langchain_google_vertexai/_anthropic_utils.py index 208a5da5d..5964376a9 100644 --- a/libs/vertexai/langchain_google_vertexai/_anthropic_utils.py +++ b/libs/vertexai/langchain_google_vertexai/_anthropic_utils.py @@ -640,7 +640,7 @@ def _tools_in_params(params: dict) -> bool: def _thinking_in_params(params: dict) -> bool: - return params.get("thinking", {}).get("type") == "enabled" + return params.get("thinking", {}).get("type") in ("enabled", "adaptive") def _documents_in_params(params: dict) -> bool: diff --git a/libs/vertexai/tests/unit_tests/test_anthropic_utils.py b/libs/vertexai/tests/unit_tests/test_anthropic_utils.py index c0e328b31..58745257f 100644 --- a/libs/vertexai/tests/unit_tests/test_anthropic_utils.py +++ b/libs/vertexai/tests/unit_tests/test_anthropic_utils.py @@ -958,6 +958,17 @@ def test_thinking_in_params_false_different_type() -> None: assert not _thinking_in_params(params) +def test_thinking_in_params_adaptive() -> None: + """`adaptive` is Anthropic's recommended thinking mode for Claude 4.6+; + Claude Opus 4.7 supports it exclusively. `_thinking_in_params` must + treat it as thinking-enabled so callers (e.g. `_stream` / `_generate` + in `model_garden`) keep `coerce_content_to_string` set to `False` and + preserve the response's structured thinking blocks.""" + params = {"thinking": {"type": "adaptive"}} + + assert _thinking_in_params(params) + + def test_documents_in_params_true() -> None: """Test _documents_in_params when document with citations is enabled.""" params = {