From b4f8b4b678f347c84f45a4eb64797324c37799f6 Mon Sep 17 00:00:00 2001 From: kamilbenkirane Date: Sun, 22 Feb 2026 19:39:11 +0100 Subject: [PATCH] refactor: remove _build_headers() side effect in Anthropic client (#159) Move request_body.pop("_beta_features") to the two call sites (_make_request, _make_stream_request) so _build_headers() becomes a pure function. Also switches _build_headers() to use _json_headers() from APIMixin as its base, removing the duplicated auth + Content-Type header construction and the ApplicationMimeType import. --- .../providers/anthropic/messages/client.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/celeste/providers/anthropic/messages/client.py b/src/celeste/providers/anthropic/messages/client.py index fcb7a886..634ab01e 100644 --- a/src/celeste/providers/anthropic/messages/client.py +++ b/src/celeste/providers/anthropic/messages/client.py @@ -6,7 +6,6 @@ from celeste.client import APIMixin from celeste.core import UsageField from celeste.io import FinishReason -from celeste.mime_types import ApplicationMimeType from celeste.providers.google.auth import GoogleADC from . import config @@ -56,23 +55,18 @@ def _build_url(self, endpoint: str, streaming: bool = False) -> str: ) return f"{config.BASE_URL}{endpoint}" - def _build_headers(self, request_body: dict[str, Any]) -> dict[str, str]: - """Build headers with beta features extracted from request.""" - beta_features: list[str] = request_body.pop("_beta_features", []) - + def _build_headers(self, beta_features: list[str] | None = None) -> dict[str, str]: + """Build Anthropic request headers.""" headers: dict[str, str] = { - **self.auth.get_headers(), + **self._json_headers(), config.HEADER_ANTHROPIC_VERSION: config.ANTHROPIC_VERSION, - "Content-Type": ApplicationMimeType.JSON, } - if beta_features: beta_values = [ getattr(config, f"BETA_{f.upper().replace('-', '_')}") for f in beta_features ] headers[config.HEADER_ANTHROPIC_BETA] = ",".join(beta_values) - return headers def _build_request( @@ -103,7 +97,8 @@ async def _make_request( if "max_tokens" not in request_body: request_body["max_tokens"] = config.DEFAULT_MAX_TOKENS - headers = self._build_headers(request_body) + beta_features: list[str] = request_body.pop("_beta_features", []) + headers = self._build_headers(beta_features=beta_features) if endpoint is None: endpoint = config.AnthropicMessagesEndpoint.CREATE_MESSAGE @@ -129,7 +124,8 @@ def _make_stream_request( if "max_tokens" not in request_body: request_body["max_tokens"] = config.DEFAULT_MAX_TOKENS - headers = self._build_headers(request_body) + beta_features: list[str] = request_body.pop("_beta_features", []) + headers = self._build_headers(beta_features=beta_features) if endpoint is None: endpoint = config.AnthropicMessagesEndpoint.CREATE_MESSAGE