diff --git a/src/services/chatbot.py b/src/services/chatbot.py index ca9d733..d113080 100644 --- a/src/services/chatbot.py +++ b/src/services/chatbot.py @@ -1,6 +1,7 @@ import os import json import math +import asyncio from uuid import UUID from datetime import date, datetime, timedelta, UTC from fastapi import HTTPException @@ -202,7 +203,14 @@ def chatbot_workflow(bot, now): ) ] + trimmed_messages - response = await model.ainvoke(input=input_messages, **kwargs) + # KIA API returns choices=null in streaming chunks which breaks LangChain. + # Even with streaming=False, ainvoke() still uses _astream() internally in async path. + # asyncio.to_thread forces sync model.invoke() which calls the real non-streaming endpoint. + if is_kia: + print(f"[KiaAI] Calling model via asyncio.to_thread (non-streaming) | model='{bot.model}'") + response = await asyncio.to_thread(model.invoke, input_messages) + else: + response = await model.ainvoke(input=input_messages, **kwargs) if is_kia: print(f"[KiaAI] Response received | type={type(response).__name__}")