langchain-ai/deepagents · error · TypeError
`create_summarization_middleware` expects `model` to be a `B
Error message
`create_summarization_middleware` expects `model` to be a `BaseChatModel` instance.
What it means
`create_summarization_middleware` builds defaults from the model's profile, so it requires an actual `BaseChatModel` instance. Passing anything else (a model name string, a callable/factory, None) raises `TypeError` at the isinstance check.
Source
Thrown at libs/deepagents/deepagents/middleware/summarization.py:1691
model: Resolved `BaseChatModel` instance.
Use `resolve_model()` first if needed for model strings.
backend: Backend instance for persisting conversation history.
summary_prompt: Prompt template for generating summaries.
trim_tokens_to_summarize: Max tokens to include when generating summary.
token_counter: Function to count tokens in messages.
Returns:
Configured `SummarizationMiddleware` instance.
Raises:
TypeError: If `model` is not a `BaseChatModel` instance.
"""
from langchain.chat_models import BaseChatModel as RuntimeBaseChatModel # noqa: PLC0415
if not isinstance(model, RuntimeBaseChatModel):
msg = "`create_summarization_middleware` expects `model` to be a `BaseChatModel` instance."
raise TypeError(msg)
defaults = compute_summarization_defaults(model)
return SummarizationMiddleware(
model=model,
backend=backend,
trigger=defaults["trigger"],
keep=defaults["keep"],
token_counter=token_counter,
summary_prompt=summary_prompt,
trim_tokens_to_summarize=trim_tokens_to_summarize,
truncate_args_settings=defaults["truncate_args_settings"],
)
def create_summarization_tool_middleware(
model: str | BaseChatModel,
backend: BackendProtocol,
*,View on GitHub (pinned to a1af029e6e)
Solutions
- Instantiate the model first, e.g. `init_chat_model("openai:gpt-4.1")` or `ChatOpenAI(model=...)`, and pass the instance
- Check the caller (e.g. `create_deep_agent`) isn't forwarding a raw config string
- Resolve string identifiers to instances before calling the factory
Example fix
// before
create_summarization_middleware(model="anthropic:claude-sonnet-4-5")
// after
from langchain.chat_models import init_chat_model
create_summarization_middleware(model=init_chat_model("anthropic:claude-sonnet-4-5")) Defensive patterns
Strategy: type-guard
Validate before calling
from langchain.chat_models import BaseChatModel
if not isinstance(model, BaseChatModel):
model = init_chat_model(model) # resolve string ids to instances Type guard
def is_chat_model(model: object) -> bool:
return isinstance(model, BaseChatModel) Try / catch
try:
mw = create_summarization_middleware(model=model)
except TypeError as e:
if "BaseChatModel" in str(e):
model = init_chat_model(model) # if model was a string id
else:
raise Prevention
- Pass instantiated chat models everywhere, not name strings
- Centralize model construction in one factory function
- Add isinstance assertions at configuration boundaries
When it happens
Trigger: Calling `create_summarization_middleware(model="openai:gpt-4.1", ...)` with a string identifier or other non-instance value instead of an instantiated chat model.
Common situations: Confusing the string-model convention accepted elsewhere (e.g. spec `model` fields) with this factory; passing a lazy model factory; refactors that replaced instances with model names.
Understand the failure class
Background: "Wrong argument type", "must be a string", "expected Array or Prism::Scope": TypeError and ArgumentError when a library receives a value of the wrong type — this error's family across 28 libraries.
Related errors
- `history_path_prefix` was removed in deepagents 0.7. Configu
- system_prompt must be str or None, got {type(system_prompt).
- Received unsupported arguments: {list(kwargs.keys())}
- Conversation history was offloaded to {file_path}, but {fail
- modes can only be provided when agent is a factory
AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29).
Data as JSON: /api/errors/c1d8ec654b86407f.
Report an issue: GitHub.