style: trim verbose comments in _sanitize_messages

This commit is contained in:
Re-bin
2026-02-20 11:27:21 +00:00
parent 0c2fea6d33
commit 5cc019bf1a

View File

@@ -12,9 +12,7 @@ from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
from nanobot.providers.registry import find_by_model, find_gateway from nanobot.providers.registry import find_by_model, find_gateway
# Keys that are part of the OpenAI chat-completion message schema. # Standard OpenAI chat-completion message keys; extras (e.g. reasoning_content) are stripped for strict providers.
# Anything else (e.g. reasoning_content, timestamp) is stripped before sending
# to avoid "Unrecognized chat message" errors from strict providers like StepFun.
_ALLOWED_MSG_KEYS = frozenset({"role", "content", "tool_calls", "tool_call_id", "name"}) _ALLOWED_MSG_KEYS = frozenset({"role", "content", "tool_calls", "tool_call_id", "name"})
@@ -155,13 +153,7 @@ class LiteLLMProvider(LLMProvider):
@staticmethod @staticmethod
def _sanitize_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]: def _sanitize_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
"""Strip non-standard keys from messages for strict providers. """Strip non-standard keys and ensure assistant messages have a content key."""
Some providers (e.g. StepFun via OpenRouter) reject messages that
contain extra keys like ``reasoning_content``. This method keeps
only the keys defined in the OpenAI chat-completion schema and
ensures every assistant message has a ``content`` key.
"""
sanitized = [] sanitized = []
for msg in messages: for msg in messages:
clean = {k: v for k, v in msg.items() if k in _ALLOWED_MSG_KEYS} clean = {k: v for k, v in msg.items() if k in _ALLOWED_MSG_KEYS}