Skip to content

Commit 4a251be

Browse files
committed
Only pass conv history to those that need it
1 parent d5ee6e7 commit 4a251be

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/guardrails/agents.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,12 @@ class ConversationContextWrapper:
191191

192192
def __init__(self, base: Any, history: list) -> None:
193193
self._base = base
194-
self._conversation_history = history
194+
# Expose conversation_history as public attribute per GuardrailLLMContextProto
195+
self.conversation_history = history
195196

196197
def get_conversation_history(self) -> list:
197198
"""Return conversation history for conversation-aware guardrails."""
198-
return self._conversation_history
199+
return self.conversation_history
199200

200201
def __getattr__(self, name: str) -> Any:
201202
"""Delegate all other attribute access to the base context."""

src/guardrails/client.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,16 @@ def _run_stage_guardrails(
770770

771771
async def _run_async():
772772
ctx = self.context
773+
774+
# Only wrap context with conversation history if any guardrail in this stage needs it
773775
if conversation_history:
774-
ctx = self._create_context_with_conversation(conversation_history)
776+
needs_conversation = any(
777+
getattr(g.definition, "metadata", None)
778+
and g.definition.metadata.uses_conversation_history
779+
for g in self.guardrails[stage_name]
780+
)
781+
if needs_conversation:
782+
ctx = self._create_context_with_conversation(conversation_history)
775783

776784
results = await run_guardrails(
777785
ctx=ctx,

tests/unit/test_agents.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,10 @@ async def fake_run_guardrails(**kwargs: Any) -> list[GuardrailResult]:
10951095
# Verify the context has the get_conversation_history method
10961096
assert hasattr(captured_context, "get_conversation_history") # noqa: S101
10971097

1098-
# Verify conversation history is present
1098+
# Verify conversation_history is accessible as an attribute (per GuardrailLLMContextProto)
1099+
assert hasattr(captured_context, "conversation_history") # noqa: S101
1100+
1101+
# Verify conversation history is present via method
10991102
conversation_history = captured_context.get_conversation_history()
11001103
assert len(conversation_history) == 3 # noqa: S101
11011104
assert conversation_history[0]["role"] == "user" # noqa: S101
@@ -1104,6 +1107,9 @@ async def fake_run_guardrails(**kwargs: Any) -> list[GuardrailResult]:
11041107
assert conversation_history[2]["role"] == "user" # noqa: S101
11051108
assert conversation_history[2]["content"] == "Thanks!" # noqa: S101
11061109

1110+
# Verify conversation history is also accessible via direct attribute access
1111+
assert captured_context.conversation_history == conversation_history # noqa: S101
1112+
11071113

11081114
@pytest.mark.asyncio
11091115
async def test_agent_guardrail_with_empty_conversation_history(monkeypatch: pytest.MonkeyPatch) -> None:
@@ -1142,10 +1148,16 @@ async def fake_run_guardrails(**kwargs: Any) -> list[GuardrailResult]:
11421148
# Verify the context has the get_conversation_history method
11431149
assert hasattr(captured_context, "get_conversation_history") # noqa: S101
11441150

1145-
# Verify conversation history is empty but accessible
1151+
# Verify conversation_history is accessible as an attribute (per GuardrailLLMContextProto)
1152+
assert hasattr(captured_context, "conversation_history") # noqa: S101
1153+
1154+
# Verify conversation history is empty but accessible via method
11461155
conversation_history = captured_context.get_conversation_history()
11471156
assert conversation_history == [] # noqa: S101
11481157

1158+
# Verify conversation history is also accessible via direct attribute access
1159+
assert captured_context.conversation_history == [] # noqa: S101
1160+
11491161

11501162
# =============================================================================
11511163
# Tests for updated tool-level guardrail behavior (stage_name)

0 commit comments

Comments
 (0)