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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "uipath-langchain"
version = "0.20.2"
version = "0.20.3"
description = "Python SDK that enables developers to build and deploy LangGraph agents to the UiPath Cloud Platform"
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.11"
Expand Down
101 changes: 68 additions & 33 deletions src/uipath_langchain/guardrails/middlewares/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
registry_references,
tool_result_references,
)
from ._spans import (
guardrail_action_span,
guardrail_stage,
guardrails_in_use,
register_guardrail_node,
)
from ._utils import (
convert_block_exception,
create_modified_tool_request,
Expand Down Expand Up @@ -362,7 +368,10 @@ def _handle_validation_result(
)
)
try:
return self.action.handle_validation_result(result, input_data, self._name)
with guardrail_action_span(self._name, self.action, result):
return self.action.handle_validation_result(
result, input_data, self._name
)
finally:
_action_context.reset(token)

Expand Down Expand Up @@ -412,16 +421,17 @@ async def _run_tool_guardrail(
tool_args=input_data,
)
try:
result = await asyncio.to_thread(
self._evaluate_guardrail, input_data, attachments
)
modified_input = self._handle_validation_result(
result,
input_data,
scope=GuardrailScope.TOOL,
stage=GuardrailExecutionStage.PRE,
component=tool_name,
)
with guardrail_stage(GuardrailScope.TOOL, GuardrailExecutionStage.PRE):
result = await asyncio.to_thread(
self._evaluate_guardrail, input_data, attachments
)
modified_input = self._handle_validation_result(
result,
input_data,
scope=GuardrailScope.TOOL,
stage=GuardrailExecutionStage.PRE,
component=tool_name,
)
if modified_input is not None:
request = create_modified_tool_request(request, modified_input)
except GuardrailBlockException as exc:
Expand Down Expand Up @@ -453,19 +463,22 @@ async def _run_tool_guardrail(
tool_result=output_data,
)
try:
result = await asyncio.to_thread(
self._evaluate_guardrail, output_data, attachments
)
modified_output = self._handle_validation_result(
result,
output_data,
scope=GuardrailScope.TOOL,
stage=GuardrailExecutionStage.POST,
component=tool_name,
input_payload=json.dumps(
self._extract_tool_input_data(request)
),
)
with guardrail_stage(
GuardrailScope.TOOL, GuardrailExecutionStage.POST
):
result = await asyncio.to_thread(
self._evaluate_guardrail, output_data, attachments
)
modified_output = self._handle_validation_result(
result,
output_data,
scope=GuardrailScope.TOOL,
stage=GuardrailExecutionStage.POST,
component=tool_name,
input_payload=json.dumps(
self._extract_tool_input_data(request)
),
)
if modified_output is not None:
tool_result = create_modified_tool_result(
tool_result, modified_output
Expand All @@ -488,6 +501,7 @@ async def _run_tool_guardrail(

def _create_tool_wrap_hook(self, guardrail_name: str) -> AgentMiddleware:
"""Create a wrap_tool_call hook that delegates to _run_tool_guardrail."""
guardrails_in_use()
middleware_instance = self

async def _wrap_tool_call_func(
Expand Down Expand Up @@ -543,6 +557,11 @@ async def _before_agent_func(
)

_before_agent_func.__name__ = f"{guardrail_name}_before_agent"
register_guardrail_node(
f"{_before_agent_func.__name__}.before_agent",
GuardrailScope.AGENT,
GuardrailExecutionStage.PRE,
)
hooks.append(before_agent(_before_agent_func))

if include_post:
Expand Down Expand Up @@ -571,6 +590,11 @@ async def _after_agent_func(
)

_after_agent_func.__name__ = f"{guardrail_name}_after_agent"
register_guardrail_node(
f"{_after_agent_func.__name__}.after_agent",
GuardrailScope.AGENT,
GuardrailExecutionStage.POST,
)
hooks.append(after_agent(_after_agent_func))

elif scope == GuardrailScope.LLM:
Expand All @@ -592,6 +616,11 @@ async def _before_model_func(
)

_before_model_func.__name__ = f"{guardrail_name}_before_model"
register_guardrail_node(
f"{_before_model_func.__name__}.before_model",
GuardrailScope.LLM,
GuardrailExecutionStage.PRE,
)
hooks.append(before_model(_before_model_func))

if include_post:
Expand All @@ -615,6 +644,11 @@ async def _after_model_func(
)

_after_model_func.__name__ = f"{guardrail_name}_after_model"
register_guardrail_node(
f"{_after_model_func.__name__}.after_model",
GuardrailScope.LLM,
GuardrailExecutionStage.POST,
)
hooks.append(after_model(_after_model_func))

return hooks
Expand Down Expand Up @@ -652,15 +686,16 @@ def _check_messages(
payload = text or NO_TEXT_PLACEHOLDER

try:
result = self._evaluate_guardrail(payload, attachments)
modified_text = self._handle_validation_result(
result,
payload,
scope=scope,
stage=stage,
component=component_label(scope),
input_payload=json.dumps(input_text) if input_text else None,
)
with guardrail_stage(scope, stage):
result = self._evaluate_guardrail(payload, attachments)
modified_text = self._handle_validation_result(
result,
payload,
scope=scope,
stage=stage,
component=component_label(scope),
input_payload=json.dumps(input_text) if input_text else None,
)
if text:
_apply_text_modification(messages, text, modified_text)
except GuardrailBlockException as exc:
Expand Down
Loading
Loading