LLM-based tool selector middleware.
Return metadata that marks a model call as internal to middleware.
Initialize a chat model from any supported provider using a unified interface.
Two main use cases:
config. Makes it easy to
switch between models/providers without changing your codeRequires the integration package for the chosen model provider to be installed.
See the model_provider parameter below for specific package names
(e.g., pip install langchain-openai).
Refer to the provider integration's API reference
for supported model parameters to use as **kwargs.
Keep internal model calls out of run.messages and the raw event log.
Used by middleware that makes internal model calls and runs before built-in transformers.
For tagged events, streamed message-start events are marked as tool-role and
whole-AIMessage payloads are cleared so MessagesTransformer ignores them.
The mutated events are then dropped from the raw log.
Only events within this transformer's scope are modified.
Base middleware class for an agent.
Subclass this and implement any of the defined methods to customize agent behavior between steps in the main agent loop.
State schema for the agent.
Model request information for the agent.
Response from model execution including messages and optional structured output.
The result will usually contain a single AIMessage, but may include an additional
ToolMessage if the model used a tool for structured output.
Uses an LLM to select relevant tools before calling the main model.
When an agent has many tools available, this middleware filters them down to only the most relevant ones for the user's query. This reduces token usage and helps the main model focus on the right tools.
Behavior when the selection model keeps returning a malformed response.
Can be either:
'error': Raise a ValueError (the default).'none': Select no tools.'all': Select every available tool.list[str] of tool names to fall back to.