diff --git a/libs/genai/langchain_google_genai/_function_utils.py b/libs/genai/langchain_google_genai/_function_utils.py index 6c211c8f4..e2fc82a9d 100644 --- a/libs/genai/langchain_google_genai/_function_utils.py +++ b/libs/genai/langchain_google_genai/_function_utils.py @@ -55,6 +55,7 @@ _GoogleSearchLike = gapic.Tool.GoogleSearch | dict[str, Any] _CodeExecutionLike = gapic.CodeExecution | dict[str, Any] +_UrlContextLike = gapic.UrlContext | dict[str, Any] class _ToolDict(TypedDict): @@ -62,6 +63,7 @@ class _ToolDict(TypedDict): 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 @@ -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] @@ -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) diff --git a/libs/genai/tests/integration_tests/test_builtin_tools.py b/libs/genai/tests/integration_tests/test_builtin_tools.py new file mode 100644 index 000000000..8af94336d --- /dev/null +++ b/libs/genai/tests/integration_tests/test_builtin_tools.py @@ -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 + )