Skip to content

Commit 98bfd57

Browse files
isahers1mdrxy
andauthored
fix(core): better error message for empty var names (#32073)
Previously, we hit an index out of range error with empty variable names (accessing tag[0]), now we through a slightly nicer error --------- Co-authored-by: Mason Daugherty <[email protected]>
1 parent 427d2d6 commit 98bfd57

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

libs/core/langchain_core/utils/mustache.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ def parse_tag(template: str, l_del: str, r_del: str) -> tuple[tuple[str, str], s
150150
msg = f"unclosed tag at line {_CURRENT_LINE}"
151151
raise ChevronError(msg) from e
152152

153+
# Check for empty tags
154+
if not tag.strip():
155+
msg = f"empty tag at line {_CURRENT_LINE}"
156+
raise ChevronError(msg)
157+
153158
# Find the type meaning of the first character
154159
tag_type = tag_types.get(tag[0], "variable")
155160

libs/core/tests/unit_tests/prompts/test_structured.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from inspect import isclass
33
from typing import Any, Union, cast
44

5+
import pytest
56
from pydantic import BaseModel
67

78
from langchain_core.language_models import FakeListChatModel
@@ -10,6 +11,7 @@
1011
from langchain_core.messages import HumanMessage
1112
from langchain_core.prompts.structured import StructuredPrompt
1213
from langchain_core.runnables.base import Runnable, RunnableLambda
14+
from langchain_core.utils.mustache import ChevronError
1315
from langchain_core.utils.pydantic import is_basemodel_subclass
1416

1517

@@ -128,3 +130,8 @@ def test_structured_prompt_template_format() -> None:
128130
assert prompt.invoke({"person": {"name": "foo"}}).to_messages() == [
129131
HumanMessage("hi foo")
130132
]
133+
134+
135+
def test_structured_prompt_template_empty_vars() -> None:
136+
with pytest.raises(ChevronError, match="empty tag"):
137+
StructuredPrompt([("human", "hi {{}}")], schema={}, template_format="mustache")

0 commit comments

Comments
 (0)