Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@

_GoogleSearchLike = gapic.Tool.GoogleSearch | dict[str, Any]
_CodeExecutionLike = gapic.CodeExecution | dict[str, Any]
_UrlContextLike = gapic.UrlContext | dict[str, Any]


class _ToolDict(TypedDict):
function_declarations: Sequence[_FunctionDeclarationLike]
google_search_retrieval: _GoogleSearchRetrievalLike | None
google_search: NotRequired[_GoogleSearchLike]
code_execution: NotRequired[_CodeExecutionLike]
url_context: NotRequired[_UrlContextLike]


# Info: This means one tool=Sequence of FunctionDeclaration
Expand Down Expand Up @@ -153,6 +155,7 @@ def convert_to_genai_function_declarations(
"google_search_retrieval",
"google_search",
"code_execution",
"url_context",
]
):
fd = _format_to_gapic_function_declaration(tool) # type: ignore[arg-type]
Expand Down Expand Up @@ -186,6 +189,8 @@ def convert_to_genai_function_declarations(
)
if "code_execution" in tool:
gapic_tool.code_execution = gapic.CodeExecution(tool["code_execution"])
if "url_context" in tool:
gapic_tool.url_context = gapic.UrlContext(tool["url_context"])
else:
fd = _format_to_gapic_function_declaration(tool)
gapic_tool.function_declarations.append(fd)
Expand Down
21 changes: 21 additions & 0 deletions libs/genai/tests/integration_tests/test_builtin_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from langchain_core.messages import AIMessage

from langchain_google_genai import ChatGoogleGenerativeAI

_MODEL = "gemini-2.5-flash"


def test_url_context_tool() -> None:
model = ChatGoogleGenerativeAI(model=_MODEL)
model_with_search = model.bind_tools([{"url_context": {}}])

input = "What is this page's contents about? https://docs.langchain.com"
response = model_with_search.invoke(input)
assert isinstance(response, AIMessage)

assert (
response.response_metadata["grounding_metadata"]["grounding_chunks"][0]["web"][
"uri"
]
is not None
)