feat: add content normalization for multi-provider support

Handle Gemini's list-of-dicts response format alongside OpenAI/Anthropic strings
This commit is contained in:
MUmarJ 2026-01-16 18:53:56 -05:00
parent 13b826a31d
commit 2e4cba0094
5 changed files with 23 additions and 8 deletions

View File

@ -1,7 +1,7 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time
import json
from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_sentiment, get_insider_transactions
from tradingagents.agents.utils.agent_utils import get_fundamentals, get_balance_sheet, get_cashflow, get_income_statement, get_insider_sentiment, get_insider_transactions, normalize_content
from tradingagents.dataflows.config import get_config
@ -53,7 +53,7 @@ def create_fundamentals_analyst(llm):
report = ""
if len(result.tool_calls) == 0:
report = result.content
report = normalize_content(result.content)
return {
"messages": [result],

View File

@ -1,7 +1,7 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time
import json
from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators
from tradingagents.agents.utils.agent_utils import get_stock_data, get_indicators, normalize_content
from tradingagents.dataflows.config import get_config
@ -75,7 +75,7 @@ Volume-Based Indicators:
report = ""
if len(result.tool_calls) == 0:
report = result.content
report = normalize_content(result.content)
return {
"messages": [result],

View File

@ -1,7 +1,7 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time
import json
from tradingagents.agents.utils.agent_utils import get_news, get_global_news
from tradingagents.agents.utils.agent_utils import get_news, get_global_news, normalize_content
from tradingagents.dataflows.config import get_config
@ -48,7 +48,7 @@ def create_news_analyst(llm):
report = ""
if len(result.tool_calls) == 0:
report = result.content
report = normalize_content(result.content)
return {
"messages": [result],

View File

@ -1,7 +1,7 @@
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import time
import json
from tradingagents.agents.utils.agent_utils import get_news
from tradingagents.agents.utils.agent_utils import get_news, normalize_content
from tradingagents.dataflows.config import get_config
@ -49,7 +49,7 @@ def create_social_media_analyst(llm):
report = ""
if len(result.tool_calls) == 0:
report = result.content
report = normalize_content(result.content)
return {
"messages": [result],

View File

@ -1,5 +1,20 @@
from langchain_core.messages import HumanMessage, RemoveMessage
def normalize_content(content):
"""Normalize LLM response content to string.
Gemini returns content as a list of dicts with 'text' keys,
while OpenAI/Anthropic return a simple string.
"""
if isinstance(content, list):
return "".join(
block.get("text", "") if isinstance(block, dict) else str(block)
for block in content
)
return content
# Import tools from separate utility files
from tradingagents.agents.utils.core_stock_tools import (
get_stock_data