Skip to content

Commit 2af2c1c

Browse files
mcavdarmdrxysydney-runklelnhsingh
authored
docs: Replace ModelRequestHandler with Callable[[ModelRequest], ModelResponse] (#1136)
## Overview Replace ModelRequestHandler with Callable[[ModelRequest], ModelResponse] similar to #1123 ## Type of change **Type:** [Update existing documentation /bug/ Remove outdated content ] ## Related issues/PRs <!-- Link to related issues, feature PRs, or discussions (if applicable) To automatically close an issue when this PR is merged, use closing keywords: - "closes #123" or "fixes #123" or "resolves #123" For regular references without auto-closing, just use: - "#123" or "See issue #123" Examples: - closes #456 (will auto-close issue #456 when PR is merged) - See #789 for context (will reference but not auto-close issue #789) --> - GitHub issue: - Feature PR: See #1123 for context <!-- For LangChain employees, if applicable: --> - Linear issue: - Slack thread: ## Checklist <!-- Put an 'x' in all boxes that apply --> - [x] I have read the [contributing guidelines](README.md) - [x] I have tested my changes locally using `docs dev` - [x] All code examples have been tested and work correctly - [x] I have used **root relative** paths for internal links - [x] I have updated navigation in `src/docs.json` if needed - I have gotten approval from the relevant reviewers - (Internal team members only / optional) I have created a preview deployment using the [Create Preview Branch workflow](https://github.com/langchain-ai/docs/actions/workflows/create-preview-branch.yml) ## Additional notes <!-- Any other information that would be helpful for reviewers --> --------- Co-authored-by: Mason Daugherty <[email protected]> Co-authored-by: Mason Daugherty <[email protected]> Co-authored-by: Sydney Runkle <[email protected]> Co-authored-by: Lauren Hirata Singh <[email protected]>
1 parent d1c89f1 commit 2af2c1c

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/oss/python/migrate/langchain-v1.mdx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class CustomState(AgentState):
368368

369369
@tool # [!code highlight]
370370
def greet(
371-
runtime: ToolRuntime[CustomState]
371+
runtime: ToolRuntime[None, CustomState]
372372
) -> str:
373373
"""Use this to greet the user by name."""
374374
user_name = runtime.state.get("user_name", "Unknown") # [!code highlight]
@@ -492,24 +492,23 @@ This functionality has been ported to the middleware interface in v1.
492492
```python v1 (new)
493493
from langchain.agents import create_agent
494494
from langchain.agents.middleware import (
495-
AgentMiddleware, ModelRequest, ModelRequestHandler
495+
AgentMiddleware, ModelRequest
496496
)
497-
from langchain.messages import AIMessage
497+
from langchain.agents.middleware.types import ModelResponse
498498
from langchain_openai import ChatOpenAI
499-
499+
from typing import Callable
500500

501501
basic_model = ChatOpenAI(model="gpt-5-nano")
502502
advanced_model = ChatOpenAI(model="gpt-5")
503503

504504
class DynamicModelMiddleware(AgentMiddleware):
505505

506-
def wrap_model_call(self, request: ModelRequest, handler: ModelRequestHandler) -> AIMessage:
506+
def wrap_model_call(self, request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]) -> ModelResponse:
507507
if len(request.state.messages) > self.messages_threshold:
508508
model = advanced_model
509509
else:
510510
model = basic_model
511-
512-
return handler(request.replace(model=model))
511+
return handler(request.override(model=model))
513512

514513
def __init__(self, messages_threshold: int) -> None:
515514
self.messages_threshold = messages_threshold

0 commit comments

Comments
 (0)