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.

Implementation:HKUDS AI Trader Extract Conversation

From Leeroopedia


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

Overview

Concrete tool for extracting structured content from LangChain agent conversation responses, including final assistant text and tool messages.

Description

Two functions provide conversation extraction:

extract_conversation() parses the response dict and returns either the full message list (output_type="all") or just the final assistant content string (output_type="final"). For "final" mode, it scans messages in reverse to find the last AIMessage with non-empty content.

extract_tool_messages() filters the conversation for tool call results, identified by having a non-empty tool_call_id attribute or a name attribute without a finish_reason.

Usage

Call extract_conversation(response, "final") after each _ainvoke_with_retry() to check for the STOP_SIGNAL. Call extract_tool_messages(response) to get tool results for feeding back into the next reasoning step.

Code Reference

Source Location

  • Repository: AI-Trader
  • File: tools/general_tools.py
  • Lines: L72-131 (extract_conversation), L134-166 (extract_tool_messages)

Signature

def extract_conversation(conversation: dict, output_type: str):
    """
    Extract content from agent conversation response.

    Args:
        conversation: Response dict from agent.ainvoke()
        output_type: "all" for full message list, "final" for last assistant content

    Returns:
        List[message] if output_type="all", str or None if output_type="final"

    Raises:
        ValueError: If output_type not in {"all", "final"}
    """

def extract_tool_messages(conversation: dict):
    """
    Extract tool call result messages from conversation.

    Args:
        conversation: Response dict from agent.ainvoke()

    Returns:
        List of tool message entries
    """

Import

from tools.general_tools import extract_conversation, extract_tool_messages

I/O Contract

Inputs

Name Type Required Description
conversation dict Yes Response from agent.ainvoke() containing "messages" key
output_type str Yes (extract_conversation) "all" or "final"

Outputs

Name Type Description
extract_conversation("all") List Full list of message objects
extract_conversation("final") str or None Last assistant content string
extract_tool_messages() List Tool message entries for loop continuation

Usage Examples

Extract Final Response

from tools.general_tools import extract_conversation, extract_tool_messages

response = await self._ainvoke_with_retry(message)

# Get final assistant text for STOP_SIGNAL check
final_text = extract_conversation(response, "final")

# Get tool messages for loop continuation
tool_msgs = extract_tool_messages(response)

Related Pages

Implements Principle

Page Connections

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