From f7e3a21fbc0120c84596ba7b6a686d7b775c29b7 Mon Sep 17 00:00:00 2001 From: mateo Date: Tue, 27 Oct 2026 20:25:06 +0000 Subject: [PATCH] fix(types): type the provider_specific_fields source dict and beta row test helpers Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/anthropic/chat/transformation.py | 12 +++++------- ...test_anthropic_beta_headers_deployment_rows.py | 15 +++++++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py index 1410758a4ad..162b7190b00 100644 --- a/litellm/llms/anthropic/chat/transformation.py +++ b/litellm/llms/anthropic/chat/transformation.py @@ -2920,7 +2920,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): def _build_provider_specific_fields( self, - completion_response: dict, + completion_response: dict[str, object], citations: Sequence[object] | None, thinking_blocks: list[ChatCompletionThinkingBlock | ChatCompletionRedactedThinkingBlock] | None, web_search_results: Sequence[object] | None, @@ -2951,17 +2951,15 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig): if tool_results is not None: provider_specific_fields["tool_results"] = tool_results + container: Final = completion_response.get("container") + container_fields: Final[dict[str, object] | None] = container if isinstance(container, dict) else None if tool_results is not None and any(result.get("type") != "tool_search_tool_result" for result in tool_results): - container_id: Final = ( - completion_response.get("container", {}).get("id") - if isinstance(completion_response.get("container"), dict) - else None - ) + raw_container_id: Final = container_fields.get("id") if container_fields is not None else None + container_id: Final = raw_container_id if isinstance(raw_container_id, str) else None code_by_id: Final = self._build_code_by_id_map(tool_calls) code_interpreter_results = self._build_code_interpreter_results(tool_results, code_by_id, container_id) provider_specific_fields["code_interpreter_results"] = code_interpreter_results - container: Final = completion_response.get("container") if container is not None: provider_specific_fields["container"] = container diff --git a/tests/unit/test_anthropic_beta_headers_deployment_rows.py b/tests/unit/test_anthropic_beta_headers_deployment_rows.py index 777b01e4e53..dd123b686e1 100644 --- a/tests/unit/test_anthropic_beta_headers_deployment_rows.py +++ b/tests/unit/test_anthropic_beta_headers_deployment_rows.py @@ -33,9 +33,10 @@ def _bundled_config(monkeypatch: pytest.MonkeyPatch) -> None: anthropic_beta_headers_manager._BETA_HEADERS_CONFIG = None -def _bundled_rows() -> dict: +def _bundled_rows() -> dict[str, dict[str, str | None]]: with open(os.path.join(os.path.dirname(litellm.__file__), "anthropic_beta_headers_config.json")) as f: - return json.load(f) + rows: dict[str, dict[str, str | None]] = json.load(f) + return rows @pytest.mark.parametrize( @@ -70,17 +71,23 @@ def test_resolve_beta_provider_picks_the_row_per_deployment(provider: str, api_b def test_foundry_deployment_headers_follow_the_azure_ai_row() -> None: headers: Final = {"anthropic-beta": f"{DIRECT_ONLY_BETA},thinking-binding-controls-2026-08-01"} - result: Final = update_headers_with_filtered_beta(headers=headers, provider="anthropic", api_base=FOUNDRY_API_BASE) + result: dict[str, str] = update_headers_with_filtered_beta( + headers=headers, provider="anthropic", api_base=FOUNDRY_API_BASE + ) assert result == {"anthropic-beta": "thinking-binding-controls-2026-08-01"} def test_direct_deployment_headers_keep_every_anthropic_row_beta() -> None: headers: Final = {"anthropic-beta": f"{DIRECT_ONLY_BETA},thinking-binding-controls-2026-08-01"} - result: Final = update_headers_with_filtered_beta(headers=headers, provider="anthropic", api_base=DIRECT_API_BASE) + result: dict[str, str] = update_headers_with_filtered_beta( + headers=headers, provider="anthropic", api_base=DIRECT_API_BASE + ) assert result == {"anthropic-beta": f"{DIRECT_ONLY_BETA},thinking-binding-controls-2026-08-01"} def test_foundry_deployment_request_filters_header_and_body_betas_together() -> None: + headers: dict[str, str] + data: dict[str, object] headers, data = update_request_with_filtered_beta( headers={"anthropic-beta": DIRECT_ONLY_BETA}, request_data={"anthropic_beta": [DIRECT_ONLY_BETA, "thinking-binding-controls-2026-08-01"], "max_tokens": 8},