Skip to content

datarobot_genai.langgraph.history

history

Reconstruct prior AG-UI turns as structured LangChain messages.

The default chat-history path flattens prior turns into the text {chat_history} prompt variable, which drops tool calls. This converter rebuilds real HumanMessage / AIMessage(tool_calls=...) / ToolMessage so the model sees structured tool history. Reasoning, when carried, is already plain text in content (folded into the assistant turn during history extraction), so it needs no special handling here.

ag_ui_history_to_langchain

ag_ui_history_to_langchain(history: list[NormalizedHistoryMessage]) -> list[BaseMessage]

Convert normalized AG-UI history (from BaseAgent.history_messages) to LangChain messages, preserving tool calls and tool results.

Source code in datarobot_genai/langgraph/history.py
def ag_ui_history_to_langchain(
    history: list[NormalizedHistoryMessage],
) -> list[BaseMessage]:
    """Convert normalized AG-UI history (from ``BaseAgent.history_messages``) to
    LangChain messages, preserving tool calls and tool results.
    """
    messages: list[BaseMessage] = []
    for msg in history:
        role = msg["role"]
        # content must be "" (never None) for tool-call-only assistant turns.
        content = msg.get("content") or ""
        if role == "user":
            messages.append(HumanMessage(content=content))
        elif role == "assistant":
            messages.append(
                AIMessage(
                    content=content,
                    tool_calls=_to_langchain_tool_calls(msg.get("tool_calls")),
                )
            )
        elif role == "tool":
            messages.append(
                ToolMessage(content=content, tool_call_id=str(msg.get("tool_call_id") or ""))
            )
        elif role == "system":
            messages.append(SystemMessage(content=content))
    return messages