-
Notifications
You must be signed in to change notification settings - Fork 19k
feat(langchain): middleware support in create_agent
#32828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The latest updates on your projects. Learn more about Vercel for GitHub.
|
CodSpeed WallTime Performance ReportMerging #32828 will not alter performanceComparing
|
CodSpeed Instrumentation Performance ReportMerging #32828 will not alter performanceComparing Summary
|
e04d329
to
26bef49
Compare
messages = state["messages"] | ||
self._ensure_message_ids(messages) | ||
|
||
total_tokens = self.token_counter(messages) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to cache this or something? recomputing kind of annoying?
libs/langchain_v1/langchain/agents/middleware/human_in_the_loop.py
Outdated
Show resolved
Hide resolved
…32837) Main change here is the removal of `AgentJump`. Typing things as `AgentJump` doesn't really make sense, given that we often want to perform an agent jump **and** update the state of the graph, potentially including updates to state extensions from a given middleware. For example, previously we had: ```py def after_model(state: AgentState) -> AgentUpdate | AgentJump | None: ... ``` But what if my middleware adds a `model_calls` attribute? `AgentJump` has no way of inheriting that newly defined state, but I still might want to do something like: ```py def after_model(state: AgentState) -> AgentUpdate | AgentJump | None: return {'model_calls': X, 'jump_to': '__end__'} ``` Then the return type `AgentUpdate | AgentJump | None` doesn't represent the return value. There's no great way to type partial state updates yet, hence we use `dict[str, Any]`. This is a Python limitation. I've removed the conditional logic for signature parsing on `AgentJump` and we just always add conditional edges for middleware nodes now. Other things: * adding docstrings + other linting fixes (sort of a general cleanup of this module). * removing tool calling / model calling limits as those applications aren't fully fleshed out / we want to just publish these 3 middlewares first.
create_agent
Overview
Adding new
AgentMiddleware
primitive that supportsbefore_model
,after_model
, andprepare_model_request
hooks.This is very exciting! It makes our
create_agent
prebuilt much more extensible + capable. Still in alpha and subject to change.This is different than the initial implementation in that it:
TypedDict
forAgentState
-- dataclass subclassing is tricky w/ required values + required decoratorsmodel_settings
toModelRequest
so that we can pass through things to bind (like cache kwargs for anthropic middleware)TODOs
top prio
other ones
secondary prio
AgentUpdate
andAgentJump
, at least in Python)docs
See langchain-ai/docs#390
open questions
Lots of open questions right now, many of them inlined as comments for the short term, will catalog some more significant ones here.