core: Improve OutputParser error messaging when model output is truncated (max_tokens) (#30936)

Addresses #30158
When using the output parser—either in a chain or standalone—hitting
max_tokens triggers a misleading “missing variable” error instead of
indicating the output was truncated. This subtle bug often surfaces with
Anthropic models.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Ahmed Tammaa
2025-04-21 16:06:18 +02:00
committed by GitHub
parent 335f089d6a
commit de56c31672
2 changed files with 41 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ from collections.abc import AsyncIterator, Iterator
from typing import Any
import pytest
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, ValidationError
from langchain_core.messages import (
AIMessage,
@@ -635,3 +635,24 @@ def test_parse_with_different_pydantic_1_proper() -> None:
forecast="Sunny",
)
]
def test_max_tokens_error(caplog: Any) -> None:
parser = PydanticToolsParser(tools=[NameCollector], first_tool_only=True)
input = AIMessage(
content="",
tool_calls=[
{
"id": "call_OwL7f5PE",
"name": "NameCollector",
"args": {"names": ["suz", "jerm"]},
}
],
response_metadata={"stop_reason": "max_tokens"},
)
with pytest.raises(ValidationError):
_ = parser.invoke(input)
assert any(
"`max_tokens` stop reason" in msg and record.levelname == "ERROR"
for record, msg in zip(caplog.records, caplog.messages)
)