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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "llm-classifier"
version = "0.1.2"
version = "0.1.3"
description = "Structured LLM based classification, clustering and extraction framework that works with all major API providers"
readme = "README.md"
license = { file = "LICENSE" }
Expand Down
8 changes: 6 additions & 2 deletions src/llm_classifier/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import instructor
from pydantic import BaseModel, Field, create_model

from .prompt_utils import safe_prompt_format

T = TypeVar("T", bound=BaseModel)


Expand Down Expand Up @@ -299,7 +301,8 @@ def _build_prompt(
if not system_prompt:
system = None
else:
system = system_prompt.format(
system = safe_prompt_format(
system_prompt,
examples=examples_str,
format=format_schema,
input=input_text,
Expand All @@ -309,7 +312,8 @@ def _build_prompt(
if not user_prompt:
user = None
else:
user = user_prompt.format(
user = safe_prompt_format(
user_prompt,
examples=examples_str,
format=format_schema,
input=input_text,
Expand Down
7 changes: 5 additions & 2 deletions src/llm_classifier/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import instructor
from pydantic import BaseModel, Field, create_model

from .prompt_utils import safe_prompt_format

T = TypeVar("T", bound=BaseModel)


Expand Down Expand Up @@ -288,7 +290,8 @@ def _build_prompt(
if not system_prompt:
system = None
else:
system = system_prompt.format(
system = safe_prompt_format(
system_prompt,
format=format_schema,
n_clusters_instruction=n_clusters_instruction,
validation_rules=validation_rules,
Expand All @@ -298,7 +301,7 @@ def _build_prompt(
if not user_prompt:
user = None
else:
user = user_prompt.format(items=items_str)
user = safe_prompt_format(user_prompt, items=items_str)

return system, user

Expand Down
17 changes: 17 additions & 0 deletions src/llm_classifier/prompt_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Prompt templating helpers."""

from __future__ import annotations

from typing import Any


def safe_prompt_format(template: str, **values: Any) -> str:
"""Replace known placeholders without parsing other braces.

Unlike ``str.format``, this helper only replaces exact ``{name}`` tokens for
provided values and leaves all other braces untouched.
"""
rendered = template
for key, value in values.items():
rendered = rendered.replace(f"{{{key}}}", str(value))
return rendered
12 changes: 12 additions & 0 deletions tests/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ def test_build_prompt_includes_examples(self, mock_classifier):
assert "Great!" in system
assert "Examples" in system

def test_build_prompt_allows_literal_braces(self, mock_classifier):
"""Prompt templates with literal braces should not raise formatting errors."""
system, user = mock_classifier._build_prompt(
"payload {with} braces",
Sentiment,
system_prompt="Schema: {format} | keep literal {json}",
user_prompt="Input: {input} | keep literal {meta}",
)
assert "keep literal {json}" in system
assert "keep literal {meta}" in user
assert "payload {with} braces" in user


# ============================================================================
# Result Types Tests
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,27 @@ def test_build_prompt_validation_rules_require_all(self, mock_clusterer):
)
assert "Every reference ID must be assigned" in system

def test_build_prompt_allows_literal_braces(self, mock_clusterer):
"""Prompt templates with literal braces should not raise formatting errors."""
inputs = [(1, "Item {A}"), (2, "Item B")]
response_schema = mock_clusterer._build_cluster_schema(SimpleCluster, n_items=2)
system, user = mock_clusterer._build_prompt(
inputs,
response_schema,
n_clusters=2,
allow_overlap=False,
require_all=True,
system_prompt=(
"Schema: {format}\n"
"Clustering: {n_clusters_instruction}\n"
"Rules: {validation_rules} | literal {rules}"
),
user_prompt="Items:\n{items}\nliteral {notes}",
)
assert "literal {rules}" in system
assert "literal {notes}" in user
assert "Item {A}" in user


# ============================================================================
# Validation Tests
Expand Down
Loading