22from collections .abc import Iterable , Sequence
33from typing import TYPE_CHECKING , Any , Literal , Required , TypedDict , cast
44
5- from pydantic import BaseModel , SerializationInfo , field_serializer , model_validator
5+ from pydantic import (
6+ BaseModel ,
7+ SerializationInfo ,
8+ field_serializer ,
9+ field_validator ,
10+ model_validator ,
11+ )
612
713from ..types import (
814 AssistantMessage ,
3137 from anthropic .types .text_block_param import TextBlockParam
3238 from anthropic .types .tool_union_param import ToolUnionParam
3339 from anthropic .types .tool_use_block_param import ToolUseBlockParam
34- from httpx import Timeout as httpxTimeout
40+ from httpx2 import Timeout as httpxTimeout
3541
3642 class CompletionCreateParams (TypedDict , total = False ):
3743 messages : Required [Sequence [MessageParam ]]
3844 model : Required [ModelParam ]
3945 max_tokens : Required [int ]
4046 tools : Sequence [ToolUnionParam ]
4147 system : str | list [TextBlockParam ]
42- temperature : float
48+ extra_body : dict [str , object ]
49+ stop_sequences : Sequence [str ]
4350 timeout : float | httpxTimeout | None
4451 output_config : OutputConfigParam
4552else :
@@ -52,6 +59,7 @@ class CompletionCreateParams(TypedDict, total=False):
5259KNOWN_COMPLETION_PARAMS = frozenset (
5360 {
5461 "max_tokens" ,
62+ "stop_sequences" ,
5563 "temperature" ,
5664 "timeout" ,
5765 "tools" ,
@@ -184,9 +192,25 @@ class AnthropicChatConfigParams(_BaseModel):
184192 tools : Sequence [ToolDef ] | None = None
185193 system : str | list [SystemTextBlock ] | None = None
186194 temperature : float | None = None
195+ stop_sequences : Sequence [str ] | None = None
187196 timeout : float | httpxTimeout | None = None
188197 output_config : dict [str , object ] | None = None
189198
199+ @field_validator ("timeout" , mode = "before" )
200+ @classmethod
201+ def _coerce_timeout (cls , v : Any ) -> Any :
202+ # httpx2 mis-parses an httpx (v1) Timeout as a scalar; convert it.
203+ # isinstance, not __module__: SDKs relabel re-exported httpx classes.
204+ try :
205+ from httpx import Timeout as HttpxV1Timeout
206+ except ImportError :
207+ return v
208+ if isinstance (v , HttpxV1Timeout ):
209+ from httpx2 import Timeout
210+
211+ return Timeout (connect = v .connect , read = v .read , write = v .write , pool = v .pool )
212+ return v
213+
190214 @field_serializer ("messages" )
191215 def serialize_messages (
192216 self , values : Sequence [ChatMessage ], info : SerializationInfo
@@ -279,13 +303,19 @@ def to_anthropic(
279303 ** params ,
280304 )
281305
282- return cast (
283- "CompletionCreateParams" ,
306+ payload = cast (
307+ dict [ str , Any ] ,
284308 cast (
285309 object ,
286310 anthropic_params .model_dump (context = {"provider" : _PROVIDER }),
287311 ),
288312 )
313+ # SDK v1 dropped sampling kwargs on messages.create (TypeError). Keep the
314+ # public temperature API by forwarding it through extra_body.
315+ temperature = payload .pop ("temperature" , None )
316+ if temperature is not None :
317+ payload ["extra_body" ] = {"temperature" : temperature }
318+ return cast ("CompletionCreateParams" , cast (object , payload ))
289319
290320 @staticmethod
291321 def block_content_to_giskard (
0 commit comments