Skip to content

datarobot_genai.llama_index.history

history

Reconstruct prior AG-UI turns as structured LlamaIndex ChatMessage objects.

Fed to AgentWorkflow.run(chat_history=...) so the model sees real tool calls and tool results instead of the flattened {chat_history} text. Tool calls are carried in additional_kwargs["tool_calls"] (OpenAI wire shape) rather than a ToolCallBlock: the LiteLLM message adapter renders additional_kwargs verbatim but does not serialize ToolCallBlock. Reasoning, when carried, is plain text in content.

ag_ui_history_to_chat_messages

ag_ui_history_to_chat_messages(history: list[NormalizedHistoryMessage]) -> list[ChatMessage]

Convert normalized AG-UI history (from BaseAgent.history_messages) to LlamaIndex ChatMessage objects, preserving tool calls and tool results.

Source code in datarobot_genai/llama_index/history.py
def ag_ui_history_to_chat_messages(
    history: list[NormalizedHistoryMessage],
) -> list[ChatMessage]:
    """Convert normalized AG-UI history (from ``BaseAgent.history_messages``) to
    LlamaIndex ``ChatMessage`` objects, preserving tool calls and tool results.
    """
    messages: list[ChatMessage] = []
    for msg in history:
        role = msg["role"]
        content = msg.get("content") or ""
        if role == "user":
            messages.append(ChatMessage(role="user", content=content))
        elif role == "assistant":
            tool_calls = _to_openai_tool_calls(msg.get("tool_calls"))
            additional_kwargs = {"tool_calls": tool_calls} if tool_calls else {}
            messages.append(
                ChatMessage(role="assistant", content=content, additional_kwargs=additional_kwargs)
            )
        elif role == "tool":
            messages.append(
                ChatMessage(
                    role="tool",
                    content=content,
                    additional_kwargs={"tool_call_id": str(msg.get("tool_call_id") or "")},
                )
            )
        elif role == "system":
            messages.append(ChatMessage(role="system", content=content))
    return messages