Principle:HKUDS AI Trader Conversation Extraction
| 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:
- Extract the final assistant response to check for the finish signal
- Extract tool messages to continue the reasoning loop
- 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