Skip to content

Commit e757a35

Browse files
yanglikunmdrxy
andauthored
fix: correct imports and create_agent construction (#1558)
## Overview <!-- Brief description of what documentation is being added/updated --> ## Type of change **Type:** [Replace with: New documentation page / Update existing documentation / Fix typo/bug/link/formatting / Remove outdated content / Other] ## 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: <!-- For LangChain employees, if applicable: --> - Linear issue: - Slack thread: ## Checklist <!-- Put an 'x' in all boxes that apply --> - [ ] I have read the [contributing guidelines](README.md) - [ ] I have tested my changes locally using `docs dev` - [ ] All code examples have been tested and work correctly - [ ] I have used **root relative** paths for internal links - [ ] I have updated navigation in `src/docs.json` if needed (Internal team members only / optional): Create a preview deployment as necessary 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 <github@mdrxy.com>
1 parent 5e79fa6 commit e757a35

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/oss/langchain/middleware/custom.mdx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,30 +373,36 @@ Middleware can extend the agent's state with custom properties.
373373
<Tab title="Decorator">
374374

375375
```python
376+
from langchain.agents import create_agent
377+
from langchain.messages import HumanMessage
376378
from langchain.agents.middleware import AgentState, before_model, after_model
377379
from typing_extensions import NotRequired
378380
from typing import Any
379381
from langgraph.runtime import Runtime
380382

383+
381384
class CustomState(AgentState):
382385
model_call_count: NotRequired[int]
383386
user_id: NotRequired[str]
384387

388+
385389
@before_model(state_schema=CustomState, can_jump_to=["end"])
386390
def check_call_limit(state: CustomState, runtime: Runtime) -> dict[str, Any] | None:
387391
count = state.get("model_call_count", 0)
388392
if count > 10:
389393
return {"jump_to": "end"}
390394
return None
391395

396+
392397
@after_model(state_schema=CustomState)
393398
def increment_counter(state: CustomState, runtime: Runtime) -> dict[str, Any] | None:
394399
return {"model_call_count": state.get("model_call_count", 0) + 1}
395400

401+
396402
agent = create_agent(
397403
model="gpt-4o",
398404
middleware=[check_call_limit, increment_counter],
399-
tools=[...],
405+
tools=[],
400406
)
401407

402408
# Invoke with custom state
@@ -412,14 +418,18 @@ result = agent.invoke({
412418
<Tab title="Class">
413419

414420
```python
421+
from langchain.agents import create_agent
422+
from langchain.messages import HumanMessage
415423
from langchain.agents.middleware import AgentState, AgentMiddleware
416424
from typing_extensions import NotRequired
417425
from typing import Any
418426

427+
419428
class CustomState(AgentState):
420429
model_call_count: NotRequired[int]
421430
user_id: NotRequired[str]
422431

432+
423433
class CallCounterMiddleware(AgentMiddleware[CustomState]):
424434
state_schema = CustomState
425435

@@ -432,10 +442,11 @@ class CallCounterMiddleware(AgentMiddleware[CustomState]):
432442
def after_model(self, state: CustomState, runtime) -> dict[str, Any] | None:
433443
return {"model_call_count": state.get("model_call_count", 0) + 1}
434444

445+
435446
agent = create_agent(
436447
model="gpt-4o",
437448
middleware=[CallCounterMiddleware()],
438-
tools=[...],
449+
tools=[],
439450
)
440451

441452
# Invoke with custom state

0 commit comments

Comments
 (0)