diff --git a/llama_stack/models/llama/llama3/chat_format.py b/llama_stack/models/llama/llama3/chat_format.py index 7bb05d8db..0a973cf0c 100644 --- a/llama_stack/models/llama/llama3/chat_format.py +++ b/llama_stack/models/llama/llama3/chat_format.py @@ -8,6 +8,7 @@ import io import json import uuid from dataclasses import dataclass +from typing import Any from PIL import Image as PIL_Image @@ -184,16 +185,26 @@ class ChatFormat: content = content[: -len("<|eom_id|>")] stop_reason = StopReason.end_of_message - tool_name = None - tool_arguments = {} + tool_name: str | BuiltinTool | None = None + tool_arguments: dict[str, Any] = {} custom_tool_info = ToolUtils.maybe_extract_custom_tool_call(content) if custom_tool_info is not None: - tool_name, tool_arguments = custom_tool_info + # Type guard: ensure custom_tool_info is a tuple of correct types + if isinstance(custom_tool_info, tuple) and len(custom_tool_info) == 2: + extracted_tool_name, extracted_tool_arguments = custom_tool_info + # Handle both dict and str return types from the function + if isinstance(extracted_tool_arguments, dict): + tool_name, tool_arguments = extracted_tool_name, extracted_tool_arguments + else: + # If it's a string, treat it as a query parameter + tool_name, tool_arguments = extracted_tool_name, {"query": extracted_tool_arguments} + else: + tool_name, tool_arguments = None, {} # Sometimes when agent has custom tools alongside builin tools # Agent responds for builtin tool calls in the format of the custom tools # This code tries to handle that case - if tool_name in BuiltinTool.__members__: + if tool_name is not None and tool_name in BuiltinTool.__members__: tool_name = BuiltinTool[tool_name] if isinstance(tool_arguments, dict): tool_arguments = { diff --git a/pyproject.toml b/pyproject.toml index 72f3a323f..22ad816d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -242,7 +242,6 @@ exclude = [ "^llama_stack/distribution/store/registry\\.py$", "^llama_stack/distribution/utils/exec\\.py$", "^llama_stack/distribution/utils/prompt_for_config\\.py$", - "^llama_stack/models/llama/llama3/chat_format\\.py$", "^llama_stack/models/llama/llama3/interface\\.py$", "^llama_stack/models/llama/llama3/tokenizer\\.py$", "^llama_stack/models/llama/llama3/tool_utils\\.py$",