mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-30 01:52:18 +00:00
fix(anthropic-bridge): keep mid-turn system entries for guardrail and compact callers
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
08277000ac
commit
6c9f258658
3 changed files with 16 additions and 5 deletions
|
|
@ -507,7 +507,8 @@ class AnthropicMessagesHandler(BaseTranslation):
|
|||
chat_completion_compatible_request,
|
||||
_tool_name_mapping,
|
||||
) = LiteLLMAnthropicMessagesAdapter().translate_anthropic_to_openai(
|
||||
anthropic_message_request=cast(AnthropicMessagesRequest, data.copy())
|
||||
anthropic_message_request=cast(AnthropicMessagesRequest, data.copy()),
|
||||
preserve_midturn_system=True,
|
||||
)
|
||||
return chat_completion_compatible_request
|
||||
|
||||
|
|
|
|||
|
|
@ -422,6 +422,8 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
self,
|
||||
messages: list[AllAnthropicPassThroughMessageValues],
|
||||
model: str | None = None,
|
||||
*,
|
||||
preserve_midturn_system: bool = False,
|
||||
) -> list:
|
||||
new_messages: Final[list[AllMessageValues]] = []
|
||||
replayable_messages: Final = strip_encrypted_reasoning_blocks_from_anthropic_messages(messages)
|
||||
|
|
@ -430,8 +432,12 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
len(replayable_messages),
|
||||
)
|
||||
ordered_messages: Final = (
|
||||
*replayable_messages[:leading_count],
|
||||
*convert_mid_conversation_system_turns(replayable_messages[leading_count:]),
|
||||
replayable_messages
|
||||
if preserve_midturn_system
|
||||
else (
|
||||
*replayable_messages[:leading_count],
|
||||
*convert_mid_conversation_system_turns(replayable_messages[leading_count:]),
|
||||
)
|
||||
)
|
||||
for m in ordered_messages:
|
||||
user_message: ChatCompletionUserMessage | None = None
|
||||
|
|
@ -1166,6 +1172,7 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
anthropic_message_request: AnthropicMessagesRequest,
|
||||
*,
|
||||
custom_llm_provider: str | None = None,
|
||||
preserve_midturn_system: bool = False,
|
||||
) -> tuple[ChatCompletionRequest, dict[str, str]]:
|
||||
"""
|
||||
This is used by the beta Anthropic Adapter, for translating anthropic `/v1/messages` requests to the openai format.
|
||||
|
|
@ -1187,6 +1194,7 @@ class LiteLLMAnthropicMessagesAdapter:
|
|||
new_messages = self.translate_anthropic_messages_to_openai(
|
||||
messages=messages_list,
|
||||
model=anthropic_message_request.get("model"),
|
||||
preserve_midturn_system=preserve_midturn_system,
|
||||
)
|
||||
## ADD SYSTEM MESSAGE TO MESSAGES
|
||||
self._add_system_message_to_messages(new_messages, anthropic_message_request)
|
||||
|
|
|
|||
|
|
@ -744,7 +744,8 @@ def _count_effective_tokens(
|
|||
messages=cast(
|
||||
"list[AllAnthropicPassThroughMessageValues]",
|
||||
messages_without_compaction,
|
||||
)
|
||||
),
|
||||
preserve_midturn_system=True,
|
||||
)
|
||||
except Exception as e:
|
||||
verbose_logger.debug(
|
||||
|
|
@ -899,7 +900,8 @@ def _build_summary_messages(
|
|||
messages=cast(
|
||||
"list[AllAnthropicPassThroughMessageValues]",
|
||||
stripped,
|
||||
)
|
||||
),
|
||||
preserve_midturn_system=True,
|
||||
)
|
||||
except Exception as e:
|
||||
verbose_logger.warning(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue