Skip to content

fix: cas-langchain internal mapping not including ai/tool messages#575

Merged
maxduu merged 2 commits intomainfrom
fix-conversational-history-mapping
Feb 13, 2026
Merged

fix: cas-langchain internal mapping not including ai/tool messages#575
maxduu merged 2 commits intomainfrom
fix-conversational-history-mapping

Conversation

@maxduu
Copy link
Contributor

@maxduu maxduu commented Feb 13, 2026

The CAS messages, when being converted to LangChain messages, were always being converted to HumanMessage. This means that the input chat-history was only a list of HumanMessage, losing conversational-context / tool-calls / results. This PR:

  • Maps to AIMessage and ToolMessage as well
  • Ensures HumanMessage uses content-blocks which can be later extended for file-attachments.

Tested end-to-end with:

  • GPT
  • Claude
  • Gemini

and the models take in the Langchain messages correctly and perform as expected.

Note: Ideally we pass in content_blocks for AIMessage as well rather than string content, but when doing so, OpenAI errors unless a msg_ prefix is used for content-block IDs. When needed, we can switch to content_blocks but need to work out a common ID strategy across models for the content-block IDs. At the moment there's no use-case for passing in multiple content-blocks just yet (Our CAS Assistant messages wouldn't include other blocks right now, e.g. file-attachments or reasoning).

Example:

Input:
image

Output Before:
image

Output After:
image

In json.dumps form

=== Mapping UiPath Messages ===
  [
    {
      "message_id": "6a88cd92-a927-499f-9688-60e0c33c2352",
      "role": "user",
      "content_parts": [
        {
          "content_part_id": "2f7a0952-f97a-4c4a-bebf-438396063123",
          "mime_type": "text/plain",
          "data": {
            "inline": "What's the tallest mountain in the world?"
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "3f63ac8f-23d5-4ce5-895d-2bc0d10c14d4",
      "role": "assistant",
      "content_parts": [
        {
          "content_part_id": "eb451f7f-878b-40f8-9d83-298cf6994c70",
          "mime_type": "text/markdown",
          "data": {
            "inline": "I'll search for that information for you."
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [
        {
          "tool_call_id": "9a0c9902-9530-4982-9d33-213ea0bf90e7",
          "name": "Web Search",
          "input": {
            "provider": "GoogleCustomSearch",
            "query": "tallest mountain in the world"
          },
          "timestamp": "2026-02-13T02:02:33.086813",
          "result": {
            "timestamp": "2026-02-13T02:02:33.086813",
            "output": {
              "formattedResults": "Mount Everest is the tallest mountain in the world, with a height of 8,848 meters (29,029 feet) above sea level."
            },
            "is_error": null,
            "cancelled": null
          },
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "4ab95711-3183-408a-bdbf-95e57781b3a1",
      "role": "assistant",
      "content_parts": [
        {
          "content_part_id": "460c0d9e-2072-4df7-8201-85af9aef94f9",
          "mime_type": "text/markdown",
          "data": {
            "inline": "The tallest mountain in the world is Mount Everest, which stands at 8,848 meters (29,029 feet) above sea level."
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "ca08c47a-33c3-4e50-93f1-1ff73107eccb",
      "role": "user",
      "content_parts": [
        {
          "content_part_id": "920dfb1c-540e-48b6-a79e-1f3b227a8071",
          "mime_type": "text/plain",
          "data": {
            "inline": "What country is it located in?"
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "b313aecf-ceab-4209-8ceb-e4dcc02b91a2",
      "role": "assistant",
      "content_parts": [
        {
          "content_part_id": "90180f5a-942b-4541-901e-2d91099a59f9",
          "mime_type": "text/markdown",
          "data": {
            "inline": "I'll search for the location of Mount Everest."
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [
        {
          "tool_call_id": "f1965a86-1c34-4fab-9c8b-18c0b3b181e9",
          "name": "Web Search",
          "input": {
            "provider": "GoogleCustomSearch",
            "query": "Mount Everest location country"
          },
          "timestamp": "2026-02-13T02:02:33.086813",
          "result": {
            "timestamp": "2026-02-13T02:02:33.086813",
            "output": {
              "formattedResults": "Mount Everest is located on the border between Nepal and Tibet (an autonomous region of China)."
            },
            "is_error": null,
            "cancelled": null
          },
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "9c86fbe8-b7df-41af-a62f-89887403d503",
      "role": "assistant",
      "content_parts": [
        {
          "content_part_id": "9a88f478-1618-4924-b9d9-8c0234fba7df",
          "mime_type": "text/markdown",
          "data": {
            "inline": "Mount Everest is located on the border between Nepal and Tibet, which is an autonomous region of China. So, it's shared between two 
countries: Nepal and China."
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    },
    {
      "message_id": "966b7663-4565-4b4f-abec-43b3cb643fad",
      "role": "user",
      "content_parts": [
        {
          "content_part_id": "d6a957b2-df6b-4585-91dc-26cb59dc1db3",
          "mime_type": "text/plain",
          "data": {
            "inline": "How many people have successfully climbed it?"
          },
          "citations": [],
          "is_transcript": null,
          "is_incomplete": null,
          "name": null,
          "created_at": "2026-02-13T02:02:33.086813",
          "updated_at": "2026-02-13T02:02:33.086813"
        }
      ],
      "tool_calls": [],
      "interrupts": [],
      "created_at": "2026-02-13T02:02:33.086813",
      "updated_at": "2026-02-13T02:02:33.086813",
      "span_id": null
    }
  ]
  === Converted LangChain Messages ===
  [
    {
      "content": [
        {
          "type": "text",
          "text": "What's the tallest mountain in the world?",
          "id": "2f7a0952-f97a-4c4a-bebf-438396063123"
        }
      ],
      "additional_kwargs": {
        "message_id": "6a88cd92-a927-499f-9688-60e0c33c2352",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "human",
      "name": null,
      "id": "6a88cd92-a927-499f-9688-60e0c33c2352"
    },
    {
      "content": "I'll search for that information for you.",
      "additional_kwargs": {
        "message_id": "3f63ac8f-23d5-4ce5-895d-2bc0d10c14d4",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "ai",
      "name": null,
      "id": "3f63ac8f-23d5-4ce5-895d-2bc0d10c14d4",
      "tool_calls": [
        {
          "name": "Web_Search",
          "args": {
            "provider": "GoogleCustomSearch",
            "query": "tallest mountain in the world"
          },
          "id": "9a0c9902-9530-4982-9d33-213ea0bf90e7",
          "type": "tool_call"
        }
      ],
      "invalid_tool_calls": [],
      "usage_metadata": null
    },
    {
      "content": "{\"formattedResults\": \"Mount Everest is the tallest mountain in the world, with a height of 8,848 meters (29,029 feet) above sea level.\"}",
      "additional_kwargs": {},
      "response_metadata": {},
      "type": "tool",
      "name": null,
      "id": null,
      "tool_call_id": "9a0c9902-9530-4982-9d33-213ea0bf90e7",
      "artifact": null,
      "status": "success"
    },
    {
      "content": "The tallest mountain in the world is Mount Everest, which stands at 8,848 meters (29,029 feet) above sea level.",
      "additional_kwargs": {
        "message_id": "4ab95711-3183-408a-bdbf-95e57781b3a1",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "ai",
      "name": null,
      "id": "4ab95711-3183-408a-bdbf-95e57781b3a1",
      "tool_calls": [],
      "invalid_tool_calls": [],
      "usage_metadata": null
    },
    {
      "content": [
        {
          "type": "text",
          "text": "What country is it located in?",
          "id": "920dfb1c-540e-48b6-a79e-1f3b227a8071"
        }
      ],
      "additional_kwargs": {
        "message_id": "ca08c47a-33c3-4e50-93f1-1ff73107eccb",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "human",
      "name": null,
      "id": "ca08c47a-33c3-4e50-93f1-1ff73107eccb"
    },
    {
      "content": "I'll search for the location of Mount Everest.",
      "additional_kwargs": {
        "message_id": "b313aecf-ceab-4209-8ceb-e4dcc02b91a2",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "ai",
      "name": null,
      "id": "b313aecf-ceab-4209-8ceb-e4dcc02b91a2",
      "tool_calls": [
        {
          "name": "Web_Search",
          "args": {
            "provider": "GoogleCustomSearch",
            "query": "Mount Everest location country"
          },
          "id": "f1965a86-1c34-4fab-9c8b-18c0b3b181e9",
          "type": "tool_call"
        }
      ],
      "invalid_tool_calls": [],
      "usage_metadata": null
    },
    {
      "content": "{\"formattedResults\": \"Mount Everest is located on the border between Nepal and Tibet (an autonomous region of China).\"}",
      "additional_kwargs": {},
      "response_metadata": {},
      "type": "tool",
      "name": null,
      "id": null,
      "tool_call_id": "f1965a86-1c34-4fab-9c8b-18c0b3b181e9",
      "artifact": null,
      "status": "success"
    },
    {
      "content": "Mount Everest is located on the border between Nepal and Tibet, which is an autonomous region of China. So, it's shared between two countries: 
Nepal and China.",
      "additional_kwargs": {
        "message_id": "9c86fbe8-b7df-41af-a62f-89887403d503",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "ai",
      "name": null,
      "id": "9c86fbe8-b7df-41af-a62f-89887403d503",
      "tool_calls": [],
      "invalid_tool_calls": [],
      "usage_metadata": null
    },
    {
      "content": [
        {
          "type": "text",
          "text": "How many people have successfully climbed it?",
          "id": "d6a957b2-df6b-4585-91dc-26cb59dc1db3"
        }
      ],
      "additional_kwargs": {
        "message_id": "966b7663-4565-4b4f-abec-43b3cb643fad",
        "created_at": "2026-02-13T02:02:33.086813",
        "updated_at": "2026-02-13T02:02:33.086813"
      },
      "response_metadata": {},
      "type": "human",
      "name": null,
      "id": "966b7663-4565-4b4f-abec-43b3cb643fad"
    }
  ]

@maxduu maxduu force-pushed the fix-conversational-history-mapping branch from d327d1e to a66c0c6 Compare February 13, 2026 08:08
@maxduu maxduu requested a review from norman-le February 13, 2026 08:14
@maxduu maxduu force-pushed the fix-conversational-history-mapping branch from c0d4e87 to ac71182 Compare February 13, 2026 08:37
@maxduu maxduu force-pushed the fix-conversational-history-mapping branch from ac71182 to c417ec4 Compare February 13, 2026 19:08
if uipath_message.content_parts:
for uipath_content_part in uipath_message.content_parts:
data = uipath_content_part.data
if uipath_content_part.mime_type.startswith("text") and isinstance(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be startswith("text/")

tool_messages: list[ToolMessage] = []
if uipath_message.tool_calls:
for uipath_tool_call in uipath_message.tool_calls:
# Only keep tool-calls that have finished
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There shouldn't be any unfinished tool calls unless something is wrong with the graph, right? Might want to treat as an error instead of ignoring. Alternately include without a result, as it's actually valid data.

Copy link
Contributor Author

@maxduu maxduu Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Yeah I'll make it empty content with "error" status for the LangChain ToolMessage.

@maxduu maxduu merged commit 23dd8a4 into main Feb 13, 2026
39 checks passed
@maxduu maxduu deleted the fix-conversational-history-mapping branch February 13, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants