From e5b4b54e3ea1b4fc2d947d45df17996f66900387 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Fri, 1 Nov 2024 15:30:57 +0000 Subject: [PATCH] fix(count_tokens): correctly set beta header --- .../resources/beta/messages/messages.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/anthropic/resources/beta/messages/messages.py b/src/anthropic/resources/beta/messages/messages.py index 12a3273f..62582d47 100644 --- a/src/anthropic/resources/beta/messages/messages.py +++ b/src/anthropic/resources/beta/messages/messages.py @@ -3,6 +3,7 @@ from __future__ import annotations from typing import List, Union, Iterable +from itertools import chain from typing_extensions import Literal, overload import httpx @@ -1124,9 +1125,16 @@ def count_tokens( timeout: Override the client-level default timeout for this request, in seconds """ extra_headers = { - **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}), + **strip_not_given( + { + "anthropic-beta": ",".join(chain((str(e) for e in betas), ["token-counting-2024-11-01"])) + if is_given(betas) + else NOT_GIVEN + } + ), **(extra_headers or {}), } + extra_headers = {"anthropic-beta": "token-counting-2024-11-01", **(extra_headers or {})} return self._post( "/v1/messages/count_tokens?beta=true", body=maybe_transform( @@ -2225,9 +2233,16 @@ async def count_tokens( timeout: Override the client-level default timeout for this request, in seconds """ extra_headers = { - **strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}), + **strip_not_given( + { + "anthropic-beta": ",".join(chain((str(e) for e in betas), ["token-counting-2024-11-01"])) + if is_given(betas) + else NOT_GIVEN + } + ), **(extra_headers or {}), } + extra_headers = {"anthropic-beta": "token-counting-2024-11-01", **(extra_headers or {})} return await self._post( "/v1/messages/count_tokens?beta=true", body=await async_maybe_transform(