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
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[project]
name = "uipath-langchain"
version = "0.20.4"
version = "0.20.5"
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"
dependencies = [
"uipath>=2.15.1, <2.16.0",
"uipath>=2.15.4, <2.16.0",
"uipath-core>=0.5.34, <0.6.0",
"uipath-platform>=0.3.11, <0.4.0",
"uipath-runtime>=0.13.5, <0.14.0",
Expand Down
9 changes: 9 additions & 0 deletions src/uipath_langchain/agent/advanced/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,15 @@ def transform_output(state: BaseModel) -> dict[str, Any]:
async def generate_conversational_output(state: BaseModel) -> dict[str, Any]:
assert extract_output is not None # guarded by with_output_extraction
messages = cast(ConversationalAdvancedAgentGraphState, state).messages
# Inject the system prompt into the messages array for extraction, matching
# standard loop behavior (for advanced, the system prompt is not in the messages array for the core loop).
system_prompt = runtime_prompt.static_prompt or (
getattr(state, runtime_prompt.state_key, None)
if runtime_prompt.state_key
else None
)
if system_prompt:
messages = [SystemMessage(content=system_prompt), *messages]
return {conversational_output_key: await extract_output(messages)}

wrapper: StateGraph[Any, Any, Any, Any] = StateGraph(
Expand Down
30 changes: 28 additions & 2 deletions src/uipath_langchain/agent/react/conversational_output_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
from typing import Any, TypeVar

from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
SystemMessage,
)
from langchain_core.runnables.config import var_child_runnable_config
from pydantic import BaseModel
from uipath.agent.react import SET_CONVERSATIONAL_OUTPUT_TOOL
from uipath.agent.react.conversational_prompts import (
get_generate_output_prompt,
strip_conversational_output_hint_section,
)
from uipath.llm_client import UiPathAPIError, UiPathError
from uipath.llm_client.utils.exceptions import as_uipath_error
Expand All @@ -42,7 +48,7 @@
StateT = TypeVar("StateT", bound=AgentGraphState)


def create_conversational_output_extractor(

Check failure on line 51 in src/uipath_langchain/agent/react/conversational_output_node.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 22 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=UiPath_uipath-langchain-python&issues=AaHx-A8XkCBkLyfa8JhB&open=AaHx-A8XkCBkLyfa8JhB&pullRequest=1225
model: BaseChatModel,
agent_output_schema: type[BaseModel],
) -> Callable[[Sequence[BaseMessage]], Awaitable[dict[str, Any]]]:
Expand Down Expand Up @@ -76,8 +82,28 @@
)
output_prompt = get_generate_output_prompt()

def without_output_hint_section(
exchange_messages: Sequence[BaseMessage],
) -> Sequence[BaseMessage]:
"""Drop the output-fields section from the system message, which is only relevant for the central conversational loop."""
if not exchange_messages:
return exchange_messages
first = exchange_messages[0]
if not isinstance(first, SystemMessage) or not isinstance(first.content, str):
return exchange_messages
stripped = strip_conversational_output_hint_section(first.content)
if stripped == first.content:
return exchange_messages
return [
first.model_copy(update={"content": stripped}),
*exchange_messages[1:],
]

async def extract(exchange_messages: Sequence[BaseMessage]) -> dict[str, Any]:
messages = [*exchange_messages, HumanMessage(content=output_prompt)]
messages = [
*without_output_hint_section(exchange_messages),
HumanMessage(content=output_prompt),
]
config = config_without_streaming(var_child_runnable_config.get(None))

try:
Expand Down
176 changes: 175 additions & 1 deletion tests/agent/advanced/test_conversational_advanced_agent_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from langchain_core.runnables import RunnableLambda
from langgraph.graph import END, START, StateGraph
from pydantic import BaseModel, Field
from uipath.agent.react import SET_CONVERSATIONAL_OUTPUT_TOOL
from uipath.agent.react.conversational_prompts import get_chat_system_prompt

from uipath_langchain._utils._attachments import render_attachments_block
from uipath_langchain.agent.advanced.agent import (
Expand All @@ -23,6 +25,9 @@
from uipath_langchain.agent.advanced.types import (
ConversationalAdvancedAgentGraphState,
)
from uipath_langchain.agent.react.utils import (
build_conversational_output_args_schema,
)


class _Input(BaseModel):
Expand Down Expand Up @@ -500,7 +505,176 @@ async def record(messages: Any) -> dict[str, Any]:
}
)

assert [message.id for message in seen[0]] == ["u0", "u1", "ai-1"]
# The system prompt leads the list (see TestExtractionSeesTheSystemPrompt);
# what this test pins is that no part of the transcript is dropped.
assert [message.id for message in seen[0][1:]] == ["u0", "u1", "ai-1"]


class TestExtractionSeesTheSystemPrompt:
"""The advanced conversational loop keeps its system prompt in state and injects it per
call, so it never reaches ``state.messages``. The extraction step prepends
the system prompt to the messages array so the LLM-call for output generation has it
as context (matching standard loop logic).
"""

@pytest.mark.asyncio
async def test_a_static_prompt_is_prepended(self) -> None:
seen = await _extraction_messages(system_prompt="builder prompt")

assert isinstance(seen[0], SystemMessage)
assert seen[0].content == "builder prompt"

@pytest.mark.asyncio
async def test_a_resolved_prompt_is_prepended(self) -> None:
"""A callable prompt lives in a state field, not on the closure."""
seen = await _extraction_messages(
system_prompt=lambda args: f"system:{args['tenant']}",
input_schema=_Input,
invoke_input={
"messages": [HumanMessage(content="hi", id="u1")],
"tenant": "finance",
},
)

assert isinstance(seen[0], SystemMessage)
assert seen[0].content == "system:finance"

@pytest.mark.asyncio
async def test_an_empty_prompt_prepends_nothing(self) -> None:
seen = await _extraction_messages(system_prompt="")

assert not any(isinstance(message, SystemMessage) for message in seen)

@pytest.mark.asyncio
async def test_the_prompt_is_not_written_into_graph_state(self) -> None:
"""``transform_output`` emits ``messages[initial_message_count:]``, so a
system message persisted into state would surface to the user as a
response message."""
seen: list[list[Any]] = []

async def record(messages: Any) -> dict[str, Any]:
seen.append(list(messages))
return {"ticketId": "INC-1"}

with (
patch(
"uipath_langchain.agent.advanced.agent.create_advanced_agent",
return_value=_fake_inner_agent(),
),
patch(
"uipath_langchain.agent.advanced.agent.create_conversational_output_extractor",
return_value=record,
),
):
graph = create_conversational_advanced_agent_graph(
model=_mock_model(),
tools=[],
system_prompt="builder prompt",
backend=None,
output_schema=_OutputWithCustomFields,
).compile()
result = await graph.ainvoke(
{"messages": [HumanMessage(content="hi", id="u1")]}
)

assert isinstance(seen[0][0], SystemMessage)
assert len(result["uipath__agent_response_messages"]) == 1

@pytest.mark.asyncio
async def test_the_output_hint_section_is_stripped_before_extraction(self) -> None:
"""The prepended prompt carries the loop's output-schema section, which
tells the model there is no tool for the fields -- the opposite of what
the extraction call needs. The shared extractor has to strip it, so this
exercises the real extractor rather than a stand-in."""
model, bound = _mock_extractor_model()
loop_prompt = get_chat_system_prompt(
model="claude-3-sonnet",
system_message="builder prompt",
agent_name="Agent",
output_schema=build_conversational_output_args_schema(
_OutputWithCustomFields
),
)
assert "STRUCTURED OUTPUT FOR THIS TURN" in loop_prompt

with patch(
"uipath_langchain.agent.advanced.agent.create_advanced_agent",
return_value=_fake_inner_agent(),
):
graph = create_conversational_advanced_agent_graph(
model=model,
tools=[],
system_prompt=loop_prompt,
backend=None,
output_schema=_OutputWithCustomFields,
).compile()
await graph.ainvoke({"messages": [HumanMessage(content="hi", id="u1")]})

sent = bound.ainvoke.call_args.args[0]
assert isinstance(sent[0], SystemMessage)
assert "STRUCTURED OUTPUT FOR THIS TURN" not in sent[0].content
assert "builder prompt" in sent[0].content


async def _extraction_messages(
system_prompt: Any,
input_schema: type[BaseModel] | None = None,
invoke_input: dict[str, Any] | None = None,
) -> list[Any]:
"""Run the wrapper and return the messages the extraction call received."""
seen: list[list[Any]] = []

async def record(messages: Any) -> dict[str, Any]:
seen.append(list(messages))
return {"ticketId": "INC-1"}

with (
patch(
"uipath_langchain.agent.advanced.agent.create_advanced_agent",
return_value=_fake_inner_agent(),
),
patch(
"uipath_langchain.agent.advanced.agent.create_conversational_output_extractor",
return_value=record,
),
):
graph = create_conversational_advanced_agent_graph(
model=_mock_model(),
tools=[],
system_prompt=system_prompt,
backend=None,
input_schema=input_schema,
output_schema=_OutputWithCustomFields,
).compile()
await graph.ainvoke(
invoke_input or {"messages": [HumanMessage(content="hi", id="u1")]}
)

return seen[0]


def _mock_extractor_model() -> tuple[MagicMock, MagicMock]:
"""A model whose ``model_copy().bind_tools().ainvoke()`` chain records the
messages and answers with a set_conversational_output call, so the real
extractor can run against it. Returns (model, bound)."""
response = AIMessage(
content="",
tool_calls=[
{
"name": SET_CONVERSATIONAL_OUTPUT_TOOL.name,
"args": {"ticketId": "INC-1", "resolved": True},
"id": "call_1",
}
],
)
bound = MagicMock()
bound.ainvoke = AsyncMock(return_value=response)
non_streaming = MagicMock()
non_streaming.bind_tools = MagicMock(return_value=bound)
model = MagicMock(spec=BaseChatModel)
model.profile = None
model.model_copy = MagicMock(return_value=non_streaming)
return model, bound


def _extractor(args: dict[str, Any]) -> Any:
Expand Down
Loading
Loading