Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions flexeval/core/chat_dataset/template_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

def load_jinja2_template(template: str | PathLike[str]) -> Template:
path = Path(template)
if path.exists():
return JINJA2_ENV.from_string(path.read_text(encoding="utf-8"))
try:
if path.exists():
return JINJA2_ENV.from_string(path.read_text(encoding="utf-8"))
except OSError:
# If the template is a very long string, path.exists() emits an OSError.
pass
return JINJA2_ENV.from_string(template)


Expand Down
5 changes: 5 additions & 0 deletions tests/core/chat_dataset/test_template_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,8 @@ def test_load_jinja2_template(dummy_template_file: Path) -> None:
embed_result = template_from_string.render(name="flexeval")
assert isinstance(template_from_string, Template)
assert embed_result == "Hello flexeval!"

template_from_string = load_jinja2_template("a" * 1000)
embed_result = template_from_string.render()
assert isinstance(template_from_string, Template)
assert embed_result == "a" * 1000