Types for middleware and agents.
Destination to jump to when a middleware node returns.
Annotation used to mark state attributes as omitted from input schema.
Annotation used to mark state attributes as omitted from output schema.
Annotation used to mark state attributes as purely internal for a given middleware.
Decorator to configure hook behavior in middleware methods.
Use this decorator on before_model or after_model methods in middleware classes
to configure their behavior. Currently supports specifying which destinations they
can jump to, which establishes conditional edges in the agent graph.
Decorator used to dynamically create a middleware with the before_model hook.
Decorator used to dynamically create a middleware with the after_model hook.
Decorator used to dynamically create a middleware with the before_agent hook.
Decorator used to dynamically create a middleware with the after_agent hook.
Async version is aafter_agent.
Decorator used to dynamically generate system prompts for the model.
This is a convenience decorator that creates middleware using wrap_model_call
specifically for dynamic prompt generation. The decorated function should return
a string that will be set as the system prompt for the model request.
Create middleware with wrap_model_call hook from a function.
Converts a function with handler callback into middleware that can intercept model calls, implement retry logic, handle errors, and rewrite responses.
Create middleware with wrap_tool_call hook from a function.
Async version is awrap_tool_call.
Converts a function with handler callback into middleware that can intercept tool calls, implement retry logic, monitor execution, and modify responses.
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.
Model response with an optional 'Command' from 'wrap_model_call' middleware.
Use this to return a 'Command' alongside the model response from a 'wrap_model_call' handler. The command is applied as an additional state update after the model node completes, using the graph's reducers (e.g. 'add_messages' for the 'messages' key).
Because each 'Command' is applied through the reducer, messages in the command are added alongside the model response messages rather than replacing them. For non-reducer state fields, later commands overwrite earlier ones (outermost middleware wins over inner).
Annotation used to mark state attributes as omitted from input or output schemas.
State schema for the agent.
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.
Union type for all supported response format strategies.
TypeAlias for model call handler return value.
Middleware can return either:
ModelResponse: Full response with messages and optional structured outputAIMessage: Simplified return for simple use casesExtendedModelResponse: Response with an optional Command for additional state updates
goto, resume, and graph are not yet supported on these commands.
A NotImplementedError will be raised if you try to use them.