Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:HKUDS AI Trader Conversation Extraction

From Leeroopedia


Knowledge Sources
Domains LLM_Agents, Data_Processing
Last Updated 2026-02-09 14:00 GMT

Overview

A message parsing pattern that extracts structured content from LangChain agent conversation responses for logging and control flow.

Description

Conversation Extraction provides utilities for parsing the heterogeneous message lists returned by LangChain agent invocations. The agent returns a mix of AIMessage, ToolMessage, HumanMessage, and SystemMessage objects. These utilities extract the final assistant response (for stop signal checking) or the full conversation (for logging), and separately extract tool messages (for continuation of the reasoning loop).

This pattern is critical for the agent's multi-step reasoning loop, where tool call results from one invocation must be fed back as input to the next invocation.

Usage

Use this principle after every LLM invocation to:

  1. Extract the final assistant response to check for the finish signal
  2. Extract tool messages to continue the reasoning loop
  3. Log the full conversation for debugging and analysis

Theoretical Basis

# Pseudocode for conversation extraction
def extract_final(response):
    messages = response["messages"]
    for msg in reversed(messages):
        if is_ai_message(msg) and has_content(msg):
            return msg.content
    return None

def extract_tools(response):
    return [msg for msg in response["messages"] if is_tool_message(msg)]

Key properties:

  • Type-aware: Distinguishes between AI, Tool, Human, and System messages
  • Reverse search: Finds the final assistant message by scanning backwards
  • Selective extraction: Separate functions for final response vs. tool results

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment