{"record":{"id":"5d12027a497d9bbd","repo":"langchain-ai/langchain","slug":"unrecognized-strategy-supported-strategies-are","errorCode":null,"errorMessage":"Unrecognized {strategy=}. Supported strategies are 'last' and 'first'.","messagePattern":"Unrecognized (.+?)\\. Supported strategies are 'last' and 'first'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":1520,"sourceCode":"            max_tokens=max_tokens,\n            token_counter=list_token_counter,\n            text_splitter=text_splitter_fn,\n            partial_strategy=\"first\" if allow_partial else None,\n            end_on=end_on,\n        )\n    if strategy == \"last\":\n        return _last_max_tokens(\n            messages,\n            max_tokens=max_tokens,\n            token_counter=list_token_counter,\n            allow_partial=allow_partial,\n            include_system=include_system,\n            start_on=start_on,\n            end_on=end_on,\n            text_splitter=text_splitter_fn,\n        )\n    msg = f\"Unrecognized {strategy=}. Supported strategies are 'last' and 'first'.\"  # type: ignore[unreachable]\n    raise ValueError(msg)\n\n\n_SingleMessage = BaseMessage | str | dict[str, Any]\n_T = TypeVar(\"_T\", bound=_SingleMessage)\n# A sequence of _SingleMessage that is NOT a bare str\n_MultipleMessages = Sequence[_T]\n\n\n@overload\ndef convert_to_openai_messages(\n    messages: _SingleMessage,\n    *,\n    text_format: Literal[\"string\", \"block\"] = \"string\",\n    include_id: bool = False,\n    pass_through_unknown_blocks: bool = True,\n) -> dict[str, Any]: ...\n\n","sourceCodeStart":1502,"sourceCodeEnd":1538,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L1502-L1538","documentation":"Raised by `trim_messages` after the if-chain dispatch when `strategy` is neither 'last' nor 'first'. This is the final guard; earlier validation only covers `start_on`/`include_system`, so an unrecognized strategy reaches this line.","triggerScenarios":"Calling `trim_messages(msgs, strategy='middle')`, `strategy='last '` (trailing space), or passing a None/default value that was never overridden.","commonSituations":"Config-driven strategy strings with typos or unsupported values; code written against wrappers offering more strategies than langchain-core implements.","solutions":["Use `strategy='last'` (keep the most recent messages) or `strategy='first'` (keep the earliest)","Validate strategy against `{'last', 'first'}` in your config layer before calling"],"exampleFix":"# before\ntrim_messages(msgs, max_tokens=500, strategy='recent')\n\n# after\ntrim_messages(msgs, max_tokens=500, strategy='last')","handlingStrategy":"validation","validationCode":"def valid_strategy(s: str) -> bool:\n    return s in {'last', 'first'}\n\nassert valid_strategy(strategy), f\"strategy must be 'last' or 'first', got {strategy!r}\"","typeGuard":"def is_trim_strategy(s: object) -> bool:\n    return isinstance(s, str) and s in {'last', 'first'}","tryCatchPattern":null,"preventionTips":["Restrict strategy to an enum/Literal in your config layer","Strip whitespace on config-sourced strategy values"],"tags":["trim-messages","argument-validation","context-window"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}