Skip to content
Open
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
4 changes: 3 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ dependencies = [
"opentelemetry-sdk ~= 1.24",
"prance >= 23.6.21,< 26.7.20",
# templating
"pybars4 ~= 0.9",
"jinja2 ~= 3.1",
"nest-asyncio2 >= 1.7.4,< 2",
"scipy>=1.15.1",
Expand Down Expand Up @@ -91,6 +90,9 @@ google = [
"google-cloud-aiplatform>=1.114,<1.134",
"google-genai >= 1.51,< 2.30"
]
handlebars = [
"pybars4 ~= 0.9"
]
hugging_face = [
"transformers[torch] >= 5.5.0,< 6.0",
"sentence-transformers >= 2.2,< 6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
from collections.abc import Callable
from typing import TYPE_CHECKING, Any

from pybars import Compiler, PybarsError
from pydantic import PrivateAttr, field_validator

try:
from pybars import Compiler, PybarsError
except ImportError:
Compiler = None

class PybarsError(Exception): # type: ignore
"""Placeholder for PybarsError when pybars4 is not installed."""


from semantic_kernel.exceptions import HandlebarsTemplateRenderException, HandlebarsTemplateSyntaxError
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.prompt_template.const import HANDLEBARS_TEMPLATE_FORMAT_NAME
Expand Down Expand Up @@ -52,6 +60,11 @@ def validate_template_format(cls, v: "PromptTemplateConfig") -> "PromptTemplateC

def model_post_init(self, __context: Any) -> None:
"""Post init model."""
if Compiler is None:
raise ImportError(
"pybars4 is not installed. Please install it with `pip install semantic-kernel[handlebars]` "
"to use Handlebars prompt templates."
)
if not self.prompt_template_config.template:
self._template_compiler = None
return
Expand All @@ -77,6 +90,11 @@ async def render(self, kernel: "Kernel", arguments: "KernelArguments | None" = N
Returns:
The prompt template ready to be used for an AI request
"""
if Compiler is None:
raise ImportError(
"pybars4 is not installed. Please install it with `pip install semantic-kernel[handlebars]` "
"to use Handlebars prompt templates."
)
if not self._template_compiler:
return ""
if arguments is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ def test_init_fail():
)


def test_init_without_pybars(monkeypatch):
import semantic_kernel.prompt_template.handlebars_prompt_template as hpt

monkeypatch.setattr(hpt, "Compiler", None)
with pytest.raises(ImportError, match="pybars4 is not installed"):
HandlebarsPromptTemplate(
prompt_template_config=PromptTemplateConfig(
name="test", description="test", template="{{input}}", template_format="handlebars"
)
)


async def test_render_without_pybars(monkeypatch, kernel: Kernel):
import semantic_kernel.prompt_template.handlebars_prompt_template as hpt

template = HandlebarsPromptTemplate(
prompt_template_config=PromptTemplateConfig(
name="test", description="test", template="{{input}}", template_format="handlebars"
)
)
monkeypatch.setattr(hpt, "Compiler", None)
with pytest.raises(ImportError, match="pybars4 is not installed"):
await template.render(kernel, KernelArguments(input="world"))


def test_init_template_validation_fail():
with pytest.raises(ValueError):
HandlebarsPromptTemplate(
Expand Down
6 changes: 4 additions & 2 deletions python/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.