langchain-ai/langchain · error · ValueError

include_system parameter is only valid with strategy='last'

Error message

include_system parameter is only valid with strategy='last'

What it means

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.

Source

Thrown at libs/core/langchain_core/messages/utils.py:1447

            SystemMessage("This is a 4 token text. The full message is 10 tokens."),
            HumanMessage(
                "This is a 4 token text. The full message is 10 tokens.",
                id="first",
            ),
            AIMessage(
                [{"type": "text", "text": "This is the FIRST 4 token block."}],
                id="second",
            ),
        ]
        ```
    """
    # Validate arguments
    if start_on and strategy == "first":
        msg = "start_on parameter is only valid with strategy='last'"
        raise ValueError(msg)
    if include_system and strategy == "first":
        msg = "include_system parameter is only valid with strategy='last'"
        raise ValueError(msg)

    messages = convert_to_messages(messages)

    # Handle string shortcuts for token counter
    if isinstance(token_counter, str):
        if token_counter in _TOKEN_COUNTER_SHORTCUTS:
            actual_token_counter = _TOKEN_COUNTER_SHORTCUTS[token_counter]
        else:
            available_shortcuts = ", ".join(
                f"'{key}'" for key in _TOKEN_COUNTER_SHORTCUTS
            )
            msg = (
                f"Invalid token_counter shortcut '{token_counter}'. "
                f"Available shortcuts: {available_shortcuts}."
            )
            raise ValueError(msg)
    else:
        # Type narrowing: at this point token_counter is not a str

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Drop `include_system` when using `strategy='first'`
  2. Or use `strategy='last'` where `include_system` is supported
  3. Parameterize your wrapper so strategy-specific kwargs are only forwarded when valid

Example fix

# before
trim_messages(msgs, max_tokens=500, strategy='first', include_system=True)

# after
trim_messages(msgs, max_tokens=500, strategy='first')
# or:
# trim_messages(msgs, max_tokens=500, strategy='last', include_system=True)
Defensive patterns

Strategy: validation

Validate before calling

kwargs = dict(max_tokens=500, strategy=strategy)
if strategy == 'last' and include_system:
    kwargs['include_system'] = True
trimmed = trim_messages(msgs, **kwargs)

Type guard

def include_system_allowed(strategy: str) -> bool:
    return strategy == 'last'

Prevention

When it happens

Trigger: Calling `trim_messages(msgs, max_tokens=500, strategy='first', include_system=True)`.

Common situations: 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.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/ee0384117cdeb127. Report an issue: GitHub.