fix(brave-search): change tool name and standardize response format

- Change tool name from 'web_search' to 'web_search_brave' to avoid conflicts with Tavily
- Standardize response format to JSON with 'query' and 'top_k' fields for consistency
- Remove built_in_type parameter from ToolDef to match Tavily implementation
- Return structured dict instead of string from _clean_result_by_type method

Fixes #2606: Resolves tool name conflicts and response format inconsistencies
that were causing non-deterministic behavior and poor user experience
This commit is contained in:
skamenan7 2025-07-07 15:13:56 -04:00
parent ec2ce3ef9d
commit a0a8b86fc8

View file

@ -4,6 +4,7 @@
# This source code is licensed under the terms described in the LICENSE file in # This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree. # the root directory of this source tree.
import json
from typing import Any from typing import Any
import httpx import httpx
@ -18,7 +19,6 @@ from llama_stack.apis.tools import (
ToolRuntime, ToolRuntime,
) )
from llama_stack.distribution.request_headers import NeedsRequestProviderData from llama_stack.distribution.request_headers import NeedsRequestProviderData
from llama_stack.models.llama.datatypes import BuiltinTool
from llama_stack.providers.datatypes import ToolGroupsProtocolPrivate from llama_stack.providers.datatypes import ToolGroupsProtocolPrivate
from .config import BraveSearchToolConfig from .config import BraveSearchToolConfig
@ -54,7 +54,7 @@ class BraveSearchToolRuntimeImpl(ToolGroupsProtocolPrivate, ToolRuntime, NeedsRe
return ListToolDefsResponse( return ListToolDefsResponse(
data=[ data=[
ToolDef( ToolDef(
name="web_search", name="web_search_brave",
description="Search the web for information", description="Search the web for information",
parameters=[ parameters=[
ToolParameter( ToolParameter(
@ -63,7 +63,6 @@ class BraveSearchToolRuntimeImpl(ToolGroupsProtocolPrivate, ToolRuntime, NeedsRe
parameter_type="string", parameter_type="string",
) )
], ],
built_in_type=BuiltinTool.brave_search,
) )
] ]
) )
@ -85,9 +84,10 @@ class BraveSearchToolRuntimeImpl(ToolGroupsProtocolPrivate, ToolRuntime, NeedsRe
) )
response.raise_for_status() response.raise_for_status()
results = self._clean_brave_response(response.json()) results = self._clean_brave_response(response.json())
content_items = "\n".join([str(result) for result in results]) # Format response consistently with Tavily (JSON with query and top_k)
formatted_response = {"query": kwargs["query"], "top_k": results}
return ToolInvocationResult( return ToolInvocationResult(
content=content_items, content=json.dumps(formatted_response),
) )
def _clean_brave_response(self, search_response): def _clean_brave_response(self, search_response):
@ -143,4 +143,4 @@ class BraveSearchToolRuntimeImpl(ToolGroupsProtocolPrivate, ToolRuntime, NeedsRe
else: else:
cleaned = {k: v for k, v in results.items() if k in selected_keys} cleaned = {k: v for k, v in results.items() if k in selected_keys}
return str(cleaned) return cleaned