Skip to content

Commit ff186ee

Browse files
authored
match on gr. instead of gr (#341)
* match on gr. instead of gr * unit tests for gr prefix
1 parent 12c4377 commit ff186ee

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

guardrails/prompt/base_prompt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def substitute_constants(self, text):
5454
"documented here: https://docs.getguardrails.ai/0-2-migration/"
5555
)
5656

57-
matches = re.findall(r"\${gr.(\w+)}", text)
57+
matches = re.findall(r"\${gr\.(\w+)}", text)
5858

5959
# Substitute all occurrences of ${gr.<constant_name>}
6060
# with the value of the constant.
@@ -96,7 +96,7 @@ def get_format_instructions_idx(self, text: str) -> Optional[int]:
9696

9797
# Regex to extract first occurrence of ${gr.<constant_name>}
9898

99-
matches = re.finditer(r"\${gr.(\w+)}", text)
99+
matches = re.finditer(r"\${gr\.(\w+)}", text)
100100

101101
earliest_match_idx = None
102102
earliest_match = None

tests/unit_tests/test_prompt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from unittest import mock
55

66
import pytest
7+
from pydantic import BaseModel, Field
78

89
import guardrails as gd
910
from guardrails.utils.constants import constants
@@ -250,3 +251,22 @@ def test_uses_old_constant_schema(text, is_old_schema):
250251
https://docs.getguardrails.ai/0-2-migration/\
251252
"""
252253
)
254+
255+
256+
class TestResponse(BaseModel):
257+
grade: int = Field(description="The grade of the response")
258+
259+
260+
def test_gr_prefixed_prompt_item_passes():
261+
# From pydantic:
262+
prompt = """Give me a response to ${grade}"""
263+
264+
guard = gd.Guard.from_pydantic(output_class=TestResponse, prompt=prompt)
265+
assert len(guard.prompt.variable_names) == 1
266+
267+
268+
def test_gr_dot_prefixed_prompt_item_fails():
269+
with pytest.raises(Exception):
270+
# From pydantic:
271+
prompt = """Give me a response to ${gr.ade}"""
272+
gd.Guard.from_pydantic(output_class=TestResponse, prompt=prompt)

0 commit comments

Comments
 (0)