@@ -136,15 +136,16 @@ def __next__(self):
136136 "content_block" : initial_block ,
137137 }
138138 )
139- processed_first = (
140- LiteLLMAnthropicMessagesAdapter ().translate_streaming_openai_response_to_anthropic (
141- response = first_chunk ,
142- current_content_block_index = self .current_content_block_index ,
143- )
139+ processed_first = LiteLLMAnthropicMessagesAdapter ().translate_streaming_openai_response_to_anthropic (
140+ response = first_chunk ,
141+ current_content_block_index = self .current_content_block_index ,
144142 )
145143 # Empty / stop-only first chunk: close the block before the
146144 # terminal message_delta so the sequence stays spec-compliant.
147- if isinstance (processed_first , dict ) and processed_first .get ("type" ) == "message_delta" :
145+ if (
146+ isinstance (processed_first , dict )
147+ and processed_first .get ("type" ) == "message_delta"
148+ ):
148149 self .chunk_queue .append (
149150 {
150151 "type" : "content_block_stop" ,
@@ -198,7 +199,10 @@ def __next__(self):
198199 self .sent_content_block_finish = False
199200 return self .chunk_queue .popleft ()
200201
201- if processed_chunk ["type" ] == "message_delta" and self .sent_content_block_finish is False :
202+ if (
203+ processed_chunk ["type" ] == "message_delta"
204+ and self .sent_content_block_finish is False
205+ ):
202206 # Queue both the content_block_stop and the message_delta
203207 self .chunk_queue .append (
204208 {
@@ -239,7 +243,9 @@ def __next__(self):
239243 return {"type" : "message_stop" }
240244 raise StopIteration
241245 except Exception as e :
242- verbose_logger .error ("Anthropic Adapter - {}\n {}" .format (e , traceback .format_exc ()))
246+ verbose_logger .error (
247+ "Anthropic Adapter - {}\n {}" .format (e , traceback .format_exc ())
248+ )
243249 raise StopAsyncIteration
244250
245251 async def __anext__ (self ): # noqa: PLR0915
@@ -300,13 +306,14 @@ async def __anext__(self): # noqa: PLR0915
300306 "content_block" : initial_block ,
301307 }
302308 )
303- processed_first = (
304- LiteLLMAnthropicMessagesAdapter ().translate_streaming_openai_response_to_anthropic (
305- response = first_chunk ,
306- current_content_block_index = self .current_content_block_index ,
307- )
309+ processed_first = LiteLLMAnthropicMessagesAdapter ().translate_streaming_openai_response_to_anthropic (
310+ response = first_chunk ,
311+ current_content_block_index = self .current_content_block_index ,
308312 )
309- if isinstance (processed_first , dict ) and processed_first .get ("type" ) == "message_delta" :
313+ if (
314+ isinstance (processed_first , dict )
315+ and processed_first .get ("type" ) == "message_delta"
316+ ):
310317 self .chunk_queue .append (
311318 {
312319 "type" : "content_block_stop" ,
@@ -342,16 +349,27 @@ async def __anext__(self): # noqa: PLR0915
342349 )
343350
344351 # Check if this is a usage chunk and we have a held stop_reason chunk
345- if self .holding_stop_reason_chunk is not None and getattr (chunk , "usage" , None ) is not None :
352+ if (
353+ self .holding_stop_reason_chunk is not None
354+ and getattr (chunk , "usage" , None ) is not None
355+ ):
346356 # Merge usage into the held stop_reason chunk
347357 merged_chunk = self .holding_stop_reason_chunk .copy ()
348358 if "delta" not in merged_chunk :
349359 merged_chunk ["delta" ] = {}
350360
351361 # Add usage to the held chunk
352362 uncached_input_tokens = chunk .usage .prompt_tokens or 0
353- if hasattr (chunk .usage , "prompt_tokens_details" ) and chunk .usage .prompt_tokens_details :
354- cached_tokens = getattr (chunk .usage .prompt_tokens_details , "cached_tokens" , 0 ) or 0
363+ if (
364+ hasattr (chunk .usage , "prompt_tokens_details" )
365+ and chunk .usage .prompt_tokens_details
366+ ):
367+ cached_tokens = (
368+ getattr (
369+ chunk .usage .prompt_tokens_details , "cached_tokens" , 0
370+ )
371+ or 0
372+ )
355373 uncached_input_tokens -= cached_tokens
356374
357375 usage_dict : UsageDelta = {
@@ -363,9 +381,16 @@ async def __anext__(self): # noqa: PLR0915
363381 hasattr (chunk .usage , "_cache_creation_input_tokens" )
364382 and chunk .usage ._cache_creation_input_tokens > 0
365383 ):
366- usage_dict ["cache_creation_input_tokens" ] = chunk .usage ._cache_creation_input_tokens
367- if hasattr (chunk .usage , "_cache_read_input_tokens" ) and chunk .usage ._cache_read_input_tokens > 0 :
368- usage_dict ["cache_read_input_tokens" ] = chunk .usage ._cache_read_input_tokens
384+ usage_dict ["cache_creation_input_tokens" ] = (
385+ chunk .usage ._cache_creation_input_tokens
386+ )
387+ if (
388+ hasattr (chunk .usage , "_cache_read_input_tokens" )
389+ and chunk .usage ._cache_read_input_tokens > 0
390+ ):
391+ usage_dict ["cache_read_input_tokens" ] = (
392+ chunk .usage ._cache_read_input_tokens
393+ )
369394 merged_chunk ["usage" ] = usage_dict
370395
371396 # Queue the merged chunk and reset
@@ -400,7 +425,10 @@ async def __anext__(self): # noqa: PLR0915
400425 # Return the first queued item
401426 return self .chunk_queue .popleft ()
402427
403- if processed_chunk ["type" ] == "message_delta" and self .sent_content_block_finish is False :
428+ if (
429+ processed_chunk ["type" ] == "message_delta"
430+ and self .sent_content_block_finish is False
431+ ):
404432 # Queue both the content_block_stop and the holding chunk
405433 self .chunk_queue .append (
406434 {
@@ -409,7 +437,10 @@ async def __anext__(self): # noqa: PLR0915
409437 }
410438 )
411439 self .sent_content_block_finish = True
412- if processed_chunk .get ("delta" , {}).get ("stop_reason" ) is not None :
440+ if (
441+ processed_chunk .get ("delta" , {}).get ("stop_reason" )
442+ is not None
443+ ):
413444 self .holding_stop_reason_chunk = processed_chunk
414445 else :
415446 self .chunk_queue .append (processed_chunk )
@@ -557,7 +588,9 @@ def _should_start_new_content_block(self, chunk: "ModelResponseStream") -> bool:
557588
558589 if tool_block .get ("name" ):
559590 truncated_name = tool_block ["name" ]
560- original_name = self .tool_name_mapping .get (truncated_name , truncated_name )
591+ original_name = self .tool_name_mapping .get (
592+ truncated_name , truncated_name
593+ )
561594 tool_block ["name" ] = original_name
562595
563596 if block_type != self .current_content_block_type :
0 commit comments