mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 09:25:14 +03:00
fix(core): accept non-dict Mapping values in mustache templates (#39680)
This commit is contained in:
1 parent
4f355f38de
commit
9c21d84bcb
2 files changed
+13
-1
No files matched your search
@@ -394,7 +394,7 @@ def _get_key(
|
||||
if resolved_scope in (0, False):
|
||||
return resolved_scope
|
||||
# Move into the scope
|
||||
if isinstance(resolved_scope, dict):
|
||||
if isinstance(resolved_scope, Mapping):
|
||||
try:
|
||||
resolved_scope = resolved_scope[child]
|
||||
except (KeyError, TypeError):
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
"""Test functionality related to prompts."""
|
||||
|
||||
import re
|
||||
from collections import ChainMap, UserDict
|
||||
from tempfile import NamedTemporaryFile
|
||||
from types import MappingProxyType
|
||||
from typing import Any, Literal
|
||||
from unittest import mock
|
||||
|
||||
@@ -251,6 +253,16 @@ def test_mustache_prompt_from_template(snapshot: SnapshotAssertion) -> None:
|
||||
}
|
||||
|
||||
|
||||
def test_mustache_prompt_with_non_dict_mapping() -> None:
|
||||
"""Test mustache templates accept non-`dict` `Mapping` values."""
|
||||
template = "Hello {{user.name}}"
|
||||
prompt = PromptTemplate.from_template(template, template_format="mustache")
|
||||
|
||||
assert prompt.format(user=ChainMap({"name": "Alice"})) == "Hello Alice"
|
||||
assert prompt.format(user=UserDict({"name": "Alice"})) == "Hello Alice"
|
||||
assert prompt.format(user=MappingProxyType({"name": "Alice"})) == "Hello Alice"
|
||||
|
||||
|
||||
def test_prompt_from_template_with_partial_variables() -> None:
|
||||
"""Test prompts can be constructed from a template with partial variables."""
|
||||
# given
|
||||
|
||||
Reference in new issue
Block a user