Skip to content

Commit e737fc0

Browse files
committed
Lint / formatting
1 parent 21e95fd commit e737fc0

15 files changed

Lines changed: 368 additions & 155 deletions

File tree

litellm/integrations/prometheus.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
else:
5252
AsyncIOScheduler = Any
5353

54+
5455
class PrometheusLogger(CustomLogger):
5556
# Class variables or attributes
5657

@@ -991,9 +992,7 @@ def _inc_labeled_counter(
991992
amount: float = 1.0,
992993
) -> None:
993994
_labels = prometheus_label_factory(
994-
supported_enum_labels=self.get_labels_for_metric(
995-
metric_name=metric_name
996-
),
995+
supported_enum_labels=self.get_labels_for_metric(metric_name=metric_name),
997996
enum_values=enum_values,
998997
label_context=label_context,
999998
)
@@ -1118,7 +1117,9 @@ async def async_log_success_event(self, kwargs, response_obj, start_time, end_ti
11181117

11191118
user_api_key = hash_token(user_api_key)
11201119

1121-
label_context = PrometheusLabelFactoryContext(enum_values) #amortized per request.
1120+
label_context = PrometheusLabelFactoryContext(
1121+
enum_values
1122+
) # amortized per request.
11221123

11231124
# increment total LLM requests and spend metric
11241125
self._increment_top_level_request_and_spend_metrics(
@@ -3490,7 +3491,9 @@ def _prometheus_labels_from_context(
34903491
}
34913492

34923493
if UserAPIKeyLabelNames.END_USER.value in filtered_labels:
3493-
filtered_labels[UserAPIKeyLabelNames.END_USER.value] = ctx.get_resolved_end_user()
3494+
filtered_labels[UserAPIKeyLabelNames.END_USER.value] = (
3495+
ctx.get_resolved_end_user()
3496+
)
34943497

34953498
for sk, val in ctx._custom_by_sanitized_key.items():
34963499
if sk in supported_enum_labels:

litellm/integrations/prometheus_helpers/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def __init__(self, enum_values: UserAPIKeyLabelValues) -> None:
5151
self.enum_values = enum_values
5252
enum_dict = enum_values.model_dump()
5353
self._sanitized_enum: Dict[str, Optional[str]] = {
54-
k: _sanitize_prometheus_label_value(v)
55-
for k, v in enum_dict.items()
54+
k: _sanitize_prometheus_label_value(v) for k, v in enum_dict.items()
5655
}
5756
self._custom_by_sanitized_key: Dict[str, Optional[str]] = {}
5857
if enum_values.custom_metadata_labels is not None:

litellm/litellm_core_utils/llm_cost_calc/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ def generic_cost_per_token( # noqa: PLR0915
684684
- cache_creation
685685
- image_tokens
686686
)
687-
# Clamp to zero: inconsistent streaming usage
687+
# Clamp to zero: inconsistent streaming usage
688688
if text_tokens < 0:
689689
text_tokens = 0
690690
prompt_tokens_details["text_tokens"] = text_tokens

litellm/llms/anthropic/chat/transformation.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,7 @@ def _schema_has_unsupported_output_constraints(
380380
for key, value in schema.items():
381381
if key in {"properties", "$defs"} and isinstance(value, dict):
382382
if any(
383-
AnthropicConfig._schema_has_unsupported_output_constraints(
384-
item
385-
)
383+
AnthropicConfig._schema_has_unsupported_output_constraints(item)
386384
for item in value.values()
387385
):
388386
return True
@@ -393,9 +391,7 @@ def _schema_has_unsupported_output_constraints(
393391
return True
394392
elif key in {"anyOf", "allOf", "oneOf"} and isinstance(value, list):
395393
if any(
396-
AnthropicConfig._schema_has_unsupported_output_constraints(
397-
item
398-
)
394+
AnthropicConfig._schema_has_unsupported_output_constraints(item)
399395
for item in value
400396
):
401397
return True

litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)