mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-01 02:02:20 +00:00
fix(anthropic/messages): guard context_management polyfill with try/except
Wrap apply_context_management() in a try/except so any failure (e.g. litellm.token_counter raising on an unknown tokenizer or unexpected message format) is logged but does not crash the underlying LLM request. The polyfill is a best-effort additive feature; on failure we forward the original messages without applied edits. Co-authored-by: Yassin Kortam <yassin@berri.ai>
This commit is contained in:
parent
b976871453
commit
278242de84
1 changed files with 16 additions and 8 deletions
|
|
@ -11,6 +11,7 @@ from functools import partial
|
|||
from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast
|
||||
|
||||
import litellm
|
||||
from litellm._logging import verbose_logger
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
||||
from litellm.llms.anthropic.common_utils import (
|
||||
strip_empty_text_blocks_from_anthropic_messages,
|
||||
|
|
@ -473,14 +474,21 @@ def anthropic_messages_handler(
|
|||
apply_context_management,
|
||||
)
|
||||
|
||||
edited_messages, polyfill_applied_edits = apply_context_management(
|
||||
model=model,
|
||||
messages=_shared_kwargs["messages"],
|
||||
tools=_shared_kwargs.get("tools"),
|
||||
system=_shared_kwargs.get("system"),
|
||||
context_management_spec=context_management_spec,
|
||||
)
|
||||
_shared_kwargs["messages"] = edited_messages
|
||||
try:
|
||||
edited_messages, polyfill_applied_edits = apply_context_management(
|
||||
model=model,
|
||||
messages=_shared_kwargs["messages"],
|
||||
tools=_shared_kwargs.get("tools"),
|
||||
system=_shared_kwargs.get("system"),
|
||||
context_management_spec=context_management_spec,
|
||||
)
|
||||
_shared_kwargs["messages"] = edited_messages
|
||||
except Exception as e:
|
||||
verbose_logger.exception(
|
||||
"context_management polyfill: skipping edits due to error: %s",
|
||||
e,
|
||||
)
|
||||
polyfill_applied_edits = []
|
||||
|
||||
return (
|
||||
LiteLLMMessagesToCompletionTransformationHandler.anthropic_messages_handler(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue