diff --git a/litellm/caching/redis_semantic_cache.py b/litellm/caching/redis_semantic_cache.py index 1cb1b457cd5..bac58026f18 100644 --- a/litellm/caching/redis_semantic_cache.py +++ b/litellm/caching/redis_semantic_cache.py @@ -276,7 +276,7 @@ class RedisSemanticCache(BaseCache): @classmethod def _collect_responses_input_text(cls, value: object, prompt_parts: list[str]) -> None: - value = cls._coerce_response_input_value(value) + value = cls._function_call_as_prompt(cls._coerce_response_input_value(value)) if value is None: return @@ -292,10 +292,6 @@ class RedisSemanticCache(BaseCache): return if isinstance(value, dict): - if value.get("type") == "function_call": - prompt_parts.append(tool_call_str(value.get("name"), value.get("arguments"))) - return - content = value.get("content") if content is not None: cls._collect_responses_input_text(content, prompt_parts) @@ -323,6 +319,12 @@ class RedisSemanticCache(BaseCache): prompt_parts.append(stripped_text) return + @staticmethod + def _function_call_as_prompt(value: object) -> object: + if isinstance(value, dict) and value.get("type") == "function_call": + return tool_call_str(value.get("name"), value.get("arguments")) + return value + @staticmethod def _coerce_response_input_value(value: object) -> object: model_dump: Final = getattr(value, "model_dump", None) diff --git a/litellm/litellm_core_utils/prompt_templates/common_utils.py b/litellm/litellm_core_utils/prompt_templates/common_utils.py index 90e54750fd1..d7fe1849670 100644 --- a/litellm/litellm_core_utils/prompt_templates/common_utils.py +++ b/litellm/litellm_core_utils/prompt_templates/common_utils.py @@ -205,7 +205,11 @@ def get_str_from_messages_with_tools(messages: object) -> str: def tool_call_str(name: object, arguments: object) -> str: - return json.dumps({"name": name, "arguments": arguments}, separators=(",", ":"), default=str) + return f'{{"name":{_compact_json(name)},"arguments":{_compact_json(arguments)}}}' + + +def _compact_json(value: object) -> str: + return json.dumps(value, separators=(",", ":"), default=str) def _message_str_with_tools(message: Mapping[str, object]) -> str: @@ -234,7 +238,9 @@ def _block_str_with_tools(block: Mapping[str, object]) -> str: def _openai_tool_call_str(tool_call: Mapping[str, object]) -> str: - function: Final = _as_str_mapping(tool_call.get("function")) or {} + function: Final = _as_str_mapping(tool_call.get("function")) + if function is None: + return tool_call_str(None, None) return tool_call_str(function.get("name"), function.get("arguments"))