{"record":{"id":"ee0384117cdeb127","repo":"langchain-ai/langchain","slug":"include-system-parameter-is-only-valid-with-strate","errorCode":null,"errorMessage":"include_system parameter is only valid with strategy='last'","messagePattern":"include_system parameter is only valid with strategy='last'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":1447,"sourceCode":"            SystemMessage(\"This is a 4 token text. The full message is 10 tokens.\"),\n            HumanMessage(\n                \"This is a 4 token text. The full message is 10 tokens.\",\n                id=\"first\",\n            ),\n            AIMessage(\n                [{\"type\": \"text\", \"text\": \"This is the FIRST 4 token block.\"}],\n                id=\"second\",\n            ),\n        ]\n        ```\n    \"\"\"\n    # Validate arguments\n    if start_on and strategy == \"first\":\n        msg = \"start_on parameter is only valid with strategy='last'\"\n        raise ValueError(msg)\n    if include_system and strategy == \"first\":\n        msg = \"include_system parameter is only valid with strategy='last'\"\n        raise ValueError(msg)\n\n    messages = convert_to_messages(messages)\n\n    # Handle string shortcuts for token counter\n    if isinstance(token_counter, str):\n        if token_counter in _TOKEN_COUNTER_SHORTCUTS:\n            actual_token_counter = _TOKEN_COUNTER_SHORTCUTS[token_counter]\n        else:\n            available_shortcuts = \", \".join(\n                f\"'{key}'\" for key in _TOKEN_COUNTER_SHORTCUTS\n            )\n            msg = (\n                f\"Invalid token_counter shortcut '{token_counter}'. \"\n                f\"Available shortcuts: {available_shortcuts}.\"\n            )\n            raise ValueError(msg)\n    else:\n        # Type narrowing: at this point token_counter is not a str","sourceCodeStart":1429,"sourceCodeEnd":1465,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L1429-L1465","documentation":"Raised by `trim_messages` at argument validation: `include_system` (keep the system message above the trimmed window) is only implemented for `strategy='last'`. Passing it with `strategy='first'` is rejected up front.","triggerScenarios":"Calling `trim_messages(msgs, max_tokens=500, strategy='first', include_system=True)`.","commonSituations":"Shared trimming helpers that always set `include_system=True` regardless of strategy; migrating prompt-trimming code from 'last' to 'first' strategy without pruning keyword arguments.","solutions":["Drop `include_system` when using `strategy='first'`","Or use `strategy='last'` where `include_system` is supported","Parameterize your wrapper so strategy-specific kwargs are only forwarded when valid"],"exampleFix":"# before\ntrim_messages(msgs, max_tokens=500, strategy='first', include_system=True)\n\n# after\ntrim_messages(msgs, max_tokens=500, strategy='first')\n# or:\n# trim_messages(msgs, max_tokens=500, strategy='last', include_system=True)","handlingStrategy":"validation","validationCode":"kwargs = dict(max_tokens=500, strategy=strategy)\nif strategy == 'last' and include_system:\n    kwargs['include_system'] = True\ntrimmed = trim_messages(msgs, **kwargs)","typeGuard":"def include_system_allowed(strategy: str) -> bool:\n    return strategy == 'last'","tryCatchPattern":null,"preventionTips":["Do not share a kwargs dict between 'first' and 'last' trimming calls","Read the trim_messages signature when switching strategies"],"tags":["trim-messages","argument-validation","context-window"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}