@@ -64,7 +64,11 @@ async def lifespan(app: FastAPI):
6464def _get_message_content (msg ) -> str :
6565 """Extract text content from a LlamaIndex ChatMessage."""
6666 if hasattr (msg , "blocks" ) and msg .blocks :
67- return (msg .blocks [0 ].text or "" ) if msg .blocks else ""
67+ # Find the first block with text content (skip ToolCallBlock)
68+ for block in msg .blocks :
69+ if hasattr (block , "text" ):
70+ return block .text or ""
71+ return ""
6872 if hasattr (msg , "content" ):
6973 if isinstance (msg .content , str ):
7074 return msg .content
@@ -101,6 +105,24 @@ def _message_to_response_dict(msg):
101105 }
102106 for tc in tool_calls
103107 ]
108+ elif hasattr (tool_calls [0 ], "id" ) and hasattr (tool_calls [0 ], "function" ):
109+ # ChatCompletionMessageFunctionToolCall object
110+ msg_data ["tool_calls" ] = []
111+ for tc in tool_calls :
112+ fn = tc .function
113+ args = fn .arguments if hasattr (fn , "arguments" ) else ""
114+ if isinstance (args , dict ):
115+ args = json .dumps (args )
116+ msg_data ["tool_calls" ].append (
117+ {
118+ "id" : tc .id ,
119+ "type" : getattr (tc , "type" , "function" ),
120+ "function" : {
121+ "name" : fn .name if hasattr (fn , "name" ) else "" ,
122+ "arguments" : args ,
123+ },
124+ }
125+ )
104126 else : # dict format (e.g. from additional_kwargs)
105127 msg_data ["tool_calls" ] = []
106128 for tc in tool_calls :
0 commit comments