{"record":{"id":"c1d8ec654b86407f","repo":"langchain-ai/deepagents","slug":"create-summarization-middleware-expects-model","errorCode":null,"errorMessage":"`create_summarization_middleware` expects `model` to be a `BaseChatModel` instance.","messagePattern":"`create_summarization_middleware` expects `model` to be a `BaseChatModel` instance\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/middleware/summarization.py","lineNumber":1691,"sourceCode":"        model: Resolved `BaseChatModel` instance.\n\n            Use `resolve_model()` first if needed for model strings.\n        backend: Backend instance for persisting conversation history.\n        summary_prompt: Prompt template for generating summaries.\n        trim_tokens_to_summarize: Max tokens to include when generating summary.\n        token_counter: Function to count tokens in messages.\n\n    Returns:\n        Configured `SummarizationMiddleware` instance.\n\n    Raises:\n        TypeError: If `model` is not a `BaseChatModel` instance.\n    \"\"\"\n    from langchain.chat_models import BaseChatModel as RuntimeBaseChatModel  # noqa: PLC0415\n\n    if not isinstance(model, RuntimeBaseChatModel):\n        msg = \"`create_summarization_middleware` expects `model` to be a `BaseChatModel` instance.\"\n        raise TypeError(msg)\n\n    defaults = compute_summarization_defaults(model)\n    return SummarizationMiddleware(\n        model=model,\n        backend=backend,\n        trigger=defaults[\"trigger\"],\n        keep=defaults[\"keep\"],\n        token_counter=token_counter,\n        summary_prompt=summary_prompt,\n        trim_tokens_to_summarize=trim_tokens_to_summarize,\n        truncate_args_settings=defaults[\"truncate_args_settings\"],\n    )\n\n\ndef create_summarization_tool_middleware(\n    model: str | BaseChatModel,\n    backend: BackendProtocol,\n    *,","sourceCodeStart":1673,"sourceCodeEnd":1709,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/middleware/summarization.py#L1673-L1709","documentation":"`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.","triggerScenarios":"Calling `create_summarization_middleware(model=\"openai:gpt-4.1\", ...)` with a string identifier or other non-instance value instead of an instantiated chat model.","commonSituations":"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.","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"],"exampleFix":"// before\ncreate_summarization_middleware(model=\"anthropic:claude-sonnet-4-5\")\n// after\nfrom langchain.chat_models import init_chat_model\ncreate_summarization_middleware(model=init_chat_model(\"anthropic:claude-sonnet-4-5\"))","handlingStrategy":"type-guard","validationCode":"from langchain.chat_models import BaseChatModel\nif not isinstance(model, BaseChatModel):\n    model = init_chat_model(model)  # resolve string ids to instances","typeGuard":"def is_chat_model(model: object) -> bool:\n    return isinstance(model, BaseChatModel)","tryCatchPattern":"try:\n    mw = create_summarization_middleware(model=model)\nexcept TypeError as e:\n    if \"BaseChatModel\" in str(e):\n        model = init_chat_model(model)  # if model was a string id\n    else:\n        raise","preventionTips":["Pass instantiated chat models everywhere, not name strings","Centralize model construction in one factory function","Add isinstance assertions at configuration boundaries"],"tags":["python","summarization","typeerror","model-instance"],"backgroundTag":"wrong-argument-type","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}