wrap_model_call() — langchain Function Reference
Architecture documentation for the wrap_model_call() function in types.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 4c44eeee_7275_fead_c30b_9fbc9dff5261["wrap_model_call()"] 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"] 4c44eeee_7275_fead_c30b_9fbc9dff5261 -->|defined in| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e 500f1e07_60be_163a_141c_779d2c09ffd2["wrap_model_call()"] 500f1e07_60be_163a_141c_779d2c09ffd2 -->|calls| 4c44eeee_7275_fead_c30b_9fbc9dff5261 500f1e07_60be_163a_141c_779d2c09ffd2["wrap_model_call()"] 4c44eeee_7275_fead_c30b_9fbc9dff5261 -->|calls| 500f1e07_60be_163a_141c_779d2c09ffd2 style 4c44eeee_7275_fead_c30b_9fbc9dff5261 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain_v1/langchain/agents/middleware/types.py lines 478–571
def wrap_model_call(
self,
request: ModelRequest[ContextT],
handler: Callable[[ModelRequest[ContextT]], ModelResponse[ResponseT]],
) -> ModelResponse[ResponseT] | AIMessage | ExtendedModelResponse[ResponseT]:
"""Intercept and control model execution via handler callback.
Async version is `awrap_model_call`
The handler callback executes the model request and returns a `ModelResponse`.
Middleware can call the handler multiple times for retry logic, skip calling
it to short-circuit, or modify the request/response. Multiple middleware
compose with first in list as outermost layer.
Args:
request: Model request to execute (includes state and runtime).
handler: Callback that executes the model request and returns
`ModelResponse`.
Call this to execute the model.
Can be called multiple times for retry logic.
Can skip calling it to short-circuit.
Returns:
The model call result.
Examples:
!!! example "Retry on error"
```python
def wrap_model_call(self, request, handler):
for attempt in range(3):
try:
return handler(request)
except Exception:
if attempt == 2:
raise
```
!!! example "Rewrite response"
```python
def wrap_model_call(self, request, handler):
response = handler(request)
ai_msg = response.result[0]
return ModelResponse(
result=[AIMessage(content=f"[{ai_msg.content}]")],
structured_response=response.structured_response,
)
```
!!! example "Error to fallback"
```python
def wrap_model_call(self, request, handler):
try:
return handler(request)
except Exception:
return ModelResponse(result=[AIMessage(content="Service unavailable")])
```
!!! example "Cache/short-circuit"
```python
def wrap_model_call(self, request, handler):
if cached := get_cache(request):
return cached # Short-circuit with cached result
response = handler(request)
save_cache(request, response)
return response
```
!!! example "Simple `AIMessage` return (converted automatically)"
```python
def wrap_model_call(self, request, handler):
response = handler(request)
# Can return AIMessage directly for simple cases
return AIMessage(content="Simplified response")
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does wrap_model_call() do?
wrap_model_call() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/types.py.
Where is wrap_model_call() defined?
wrap_model_call() is defined in libs/langchain_v1/langchain/agents/middleware/types.py at line 478.
What does wrap_model_call() call?
wrap_model_call() calls 1 function(s): wrap_model_call.
What calls wrap_model_call()?
wrap_model_call() is called by 1 function(s): wrap_model_call.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free