-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add conversational-output node for low-code conversational-agent loop #965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c19f253
feat: convo agent output
maxduu e69fa94
fix: ai message logic
maxduu bd654a4
fix: simplify logic for enabled outputs
maxduu d9e119a
chore: update comment
maxduu 02e234a
Merge branch 'main' into convo-agent-output-tool-call-auto
maxduu 9302f95
fix: resolve parsing last aimessage dependent on with_conversational_…
maxduu 4ab51a3
fix: formating
maxduu 04c0dd9
Merge branch 'main' into convo-agent-output-tool-call-auto
maxduu cff4da1
fix: update dependencies
maxduu 7402851
fix: typing issues, check_stop_reason, and tests
maxduu 3e13af0
fix: add space
maxduu 2b68687
Merge branch 'main' into convo-agent-output-tool-call-auto
maxduu d8647ef
chore: bump deps
maxduu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| DEFAULT_MAX_CONSECUTIVE_THINKING_MESSAGES = 0 | ||
| DEFAULT_MAX_LLM_MESSAGES = 25 | ||
|
|
||
| UIPATH_CONVERSATIONAL_AGENT_RESPONSE_MESSAGES_FIELD = "uipath__agent_response_messages" |
101 changes: 101 additions & 0 deletions
101
src/uipath_langchain/agent/react/conversational_output_node.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """GENERATE_CONVERSATIONAL_OUTPUT node for the Agent graph. | ||
|
|
||
| This intermediate node runs after AGENT for conversational agents whose | ||
| output schema declares custom fields beyond `uipath__agent_response_messages`. | ||
| It performs a focused LLM call with only the `set_conversational_output` | ||
| tool bound and `tool_choice="any"` to extract the structured output for the turn. | ||
| """ | ||
|
|
||
| from typing import TypeVar | ||
|
|
||
| from langchain_core.language_models import BaseChatModel | ||
| from langchain_core.messages import AIMessage, HumanMessage | ||
| from langchain_core.runnables.config import var_child_runnable_config | ||
| from pydantic import BaseModel | ||
| from uipath.agent.react.conversational_prompts import ( | ||
| get_generate_output_prompt, | ||
| ) | ||
| from uipath.llm_client import UiPathAPIError, UiPathError | ||
| from uipath.llm_client.utils.exceptions import as_uipath_error | ||
| from uipath.runtime.errors import UiPathErrorCategory | ||
|
|
||
| from uipath_langchain.chat.handlers import get_payload_handler | ||
|
|
||
| from ..exceptions import AgentRuntimeError, AgentRuntimeErrorCode | ||
| from ..exceptions.licensing import raise_for_provider_http_error | ||
| from ..exceptions.llm import raise_for_llm_client_error | ||
| from ..tools.utils import config_without_streaming | ||
| from .tools.tools import create_set_conversational_output_tool | ||
| from .types import AgentGraphState | ||
|
|
||
| StateT = TypeVar("StateT", bound=AgentGraphState) | ||
|
|
||
|
|
||
| def create_conversational_output_node( | ||
| model: BaseChatModel, | ||
| agent_output_schema: type[BaseModel], | ||
| ): | ||
| """Build the focused structured-output extraction node. | ||
|
|
||
| Args: | ||
| model: The chat model to invoke for the extraction call. Reused from | ||
| the AGENT loop; rebinding is stateless. | ||
| agent_output_schema: The agent's declared output schema. Used to | ||
| construct the `set_conversational_output` tool with the | ||
| LLM-fillable fields (`uipath__agent_response_messages` stripped). | ||
| """ | ||
| set_conversational_output_tool = create_set_conversational_output_tool( | ||
| agent_output_schema | ||
| ) | ||
| # Disable streaming on this internal LLM call | ||
| non_streaming_model = model.model_copy(update={"disable_streaming": True}) | ||
| payload_handler = get_payload_handler(non_streaming_model) | ||
| binding_kwargs = payload_handler.get_tool_binding_kwargs( | ||
| tools=[set_conversational_output_tool], | ||
| tool_choice="any", | ||
| parallel_tool_calls=False, | ||
| ) | ||
| llm = non_streaming_model.bind_tools( | ||
| [set_conversational_output_tool], **binding_kwargs | ||
| ) | ||
| output_prompt = get_generate_output_prompt() | ||
|
|
||
| async def conversational_output_node(state: StateT): | ||
| # The appended HumanMessage stays local to this LLM call — only the | ||
| # response is returned to state, so the framework instruction never | ||
| # enters the persisted conversation history. | ||
| messages = [*state.messages, HumanMessage(content=output_prompt)] | ||
| config = config_without_streaming(var_child_runnable_config.get(None)) | ||
|
|
||
| try: | ||
| response = await llm.ainvoke(messages, config=config) | ||
| except UiPathAPIError as e: | ||
| # New LLM clients surface provider HTTP errors as a normalized UiPathAPIError directly. | ||
| raise_for_provider_http_error(e) | ||
| except UiPathError as e: | ||
| raise_for_llm_client_error(e) | ||
| raise | ||
| except Exception as e: | ||
| # Legacy in-repo clients (use_new_llm_clients=False) raise raw provider SDK exceptions. | ||
| # Normalize via as_uipath_error and apply the same mapping when the error is HTTP-shaped; non-HTTP errors propagate. | ||
| uipath_error = as_uipath_error(e) | ||
| if isinstance(uipath_error, UiPathAPIError): | ||
| raise_for_provider_http_error(uipath_error) | ||
| raise | ||
|
|
||
| if not isinstance(response, AIMessage): | ||
| raise AgentRuntimeError( | ||
| code=AgentRuntimeErrorCode.LLM_INVALID_RESPONSE, | ||
| title=f"Structured-output LLM returned {type(response).__name__} invalid response.", | ||
| detail=( | ||
| "The language model returned an unexpected response type. " | ||
| "If you are using a BYOM configuration, verify your model deployment.", | ||
| ), | ||
| category=UiPathErrorCategory.SYSTEM, | ||
| ) | ||
|
|
||
| payload_handler.check_stop_reason(response) | ||
|
|
||
| return {"messages": [response]} | ||
|
|
||
| return conversational_output_node | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a ToolMessage result added for this? Since it's returned into graph state, will it stay in the conversation history and get replayed on the next turn as a tool call with no matching result? Would that be an issue?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this works the same way as the
end_executiontool, where the LLM adds theAIMessagewith the tool-call inputs. The tool itself is never executed since we immediately route to the TERMINATE node, so there's noToolMessageresult.To ensure this
AIMessageis not added to chat-history, we useconfig_without_streaminghere - that makes it so it doesn't stream any events to CAS and thus won't be added to the conversation-history. This is the same pattern used for the analyze-attachments tool which also makes an LLM-call that isn't streamed to CAS.Finally, we ensure the
AIMessageisn't part of theuipath__agent_response_messagesby parsing it out here withnew_conversation_messages = state.messages[initial_count:-1].So in summary - no
ToolMessagesince the tool is never executed (route directly to TERMINATE node) and then the AIMessage is never streamed to CAS, and additionally not part of the output because we extract it out.