LangChain.NativeTool (LangChain v0.15.7)

Copy Markdown View Source

Represents built-in tools available from AI/LLM services that can be used within the LangChain framework.

Native tools are functionalities provided directly by language model services (like Google AI, OpenAI) that can be invoked by LLMs to perform specific actions or retrieve information. They are "native" because they're built into the AI service itself, rather than implemented externally.

Each native tool has:

  • A unique name that identifies it (e.g., "google_search", "code_interpreter")
  • A configuration map that contains tool-specific settings

Google Search Grounding

Starting with Gemini 2.0, Google Search is available as a native tool. This enables the model to decide when to use Google Search to enhance the factuality and recency of responses.

Google Search as a tool enables:

  • More accurate and up-to-date answers with grounding sources
  • Retrieving information from the web for further analysis
  • Finding relevant images, videos, or other media for multimodal reasoning
  • Supporting specialized tasks like coding, technical troubleshooting, etc.
alias LangChain.Chains.LLMChain
alias LangChain.Message
alias LangChain.NativeTool

model = ChatGoogleAI.new!(%{temperature: 0, stream: false, model: "gemini-2.0-flash"})

{:ok, updated_chain} =
   %{llm: model, verbose: false, stream: false}
   |> LLMChain.new!()
   |> LLMChain.add_message(
     Message.new_user!("What is the current Google stock price?")
   )
   |> LLMChain.add_tools(NativeTool.new!(%{name: "google_search", configuration: %{}}))
   |> LLMChain.run()

tool_search/1 builds a provider-neutral search tool. Add it to a chain alongside functions marked defer_loading: true (see "Tool search options" in LangChain.Function). Deferred functions are declared on every request but kept out of the model's context until the model searches for them, so the request prefix never changes and prompt caching keeps working.

chain
|> LLMChain.add_tools(NativeTool.tool_search())
|> LLMChain.add_tools(deferred_functions)

Keep at least one function, normally the most-used ones, not deferred.

A chat model that offers tool search translates the tool for its provider. A chat model without tool search falls back: it leaves the search tool out of the request and sends deferred functions as ordinary tools, so every tool is available from the start. Search items another model added to the conversation are left out, and :tool_reference parts are sent as text. A chain configured for tool search therefore runs on any chat model, which keeps fallbacks and model swaps working.

Chat modelTool searchNamespacesVerified
LangChain.ChatModels.ChatAnthropicServer-side search (:bm25 or :regex), and client-side loading with :tool_reference partsNot usedLive
LangChain.ChatModels.ChatOpenAIResponsesOpenAI's hosted search; :variant does not applyYesLive
LangChain.ChatModels.ChatReqLLM, anthropic: modelsServer-side and client-side, as ChatAnthropicNot usedLive
LangChain.ChatModels.ChatReqLLM, Anthropic models on amazon_bedrock:Server-side and client-sideNot usedRequest encoding only
LangChain.ChatModels.ChatReqLLM, openai: modelsOpenAI's hosted search; :variant does not applyYesLive
LangChain.ChatModels.ChatReqLLM, any other providerFalls backRequest encoding
LangChain.ChatModels.ChatOpenAI, LangChain.ChatModels.ChatOpenAICompatible, LangChain.ChatModels.ChatAwsMantleFalls back. The Chat Completions API rejects the search tool.Request encoding and replay
LangChain.ChatModels.ChatDeepSeek, LangChain.ChatModels.ChatGoogleAI, LangChain.ChatModels.ChatVertexAI, LangChain.ChatModels.ChatGrok, LangChain.ChatModels.ChatMistralAI, LangChain.ChatModels.ChatOllamaAI, LangChain.ChatModels.ChatOrq, LangChain.ChatModels.ChatPerplexityFalls backRequest encoding
LangChain.ChatModels.ChatBumblebeeFalls backUntested

"Live" means a model searched for a deferred tool, called it, and accepted the replayed conversation on a later turn. "Request encoding" means unit tests of the request body only.

A chat model without tool search uses drop_tool_search/1 to leave the search tool out.

Summary

Functions

Remove the tool search tool from a list of tools.

Build a new native tool.

Build a new native tool and return it or raise an error if invalid.

Build the provider-neutral tool search tool.

Return true when the tool is the tool search tool built by tool_search/1.

Types

configuration()

@type configuration() :: %{required(String.t()) => any()}

t()

@type t() :: %LangChain.NativeTool{configuration: term(), id: term(), name: term()}

Functions

drop_tool_search(tools)

@spec drop_tool_search(nil | list()) :: nil | list()

Remove the tool search tool from a list of tools.

A chat model without tool search calls this before encoding its tools, so a chain configured for tool search runs on it with every function mounted. Deferred functions are left in the list and sent as ordinary tools. A debug message is logged when the search tool is removed.

new(attrs \\ %{})

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Build a new native tool.

new!(attrs \\ %{})

@spec new!(attrs :: map()) :: t() | no_return()

Build a new native tool and return it or raise an error if invalid.

tool_search(opts \\ [])

@spec tool_search(keyword()) :: t()

Build the provider-neutral tool search tool.

Options

  • :variant - How the model writes its search, on providers that offer a choice. :bm25 (default) searches with natural language queries; :regex searches with regular expressions. Providers with a single kind of search ignore it.

tool_search?(arg1)

@spec tool_search?(any()) :: boolean()

Return true when the tool is the tool search tool built by tool_search/1.