🐛 fix(chat.py): change data to error in yield statement when flow_id is invalid

🐛 fix(chat.py): catch exception when building graph and yield error message
When the flow_id is invalid, the yield statement should return an error message instead of data. Additionally, when building the graph, an exception could occur, so we added a try-except block to catch any exceptions and yield an error message instead of data.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-21 14:04:45 -03:00
commit 0fb33825d9

View file

@ -75,7 +75,7 @@ async def stream_build(flow_id: str):
try:
if flow_id not in flow_data_store:
error_message = "Invalid session ID"
yield f"data: {json.dumps({'error': error_message})}\n\n"
yield f"error: {json.dumps({'error': error_message})}\n\n"
return
graph_data = flow_data_store[flow_id].get("data")
@ -86,7 +86,15 @@ async def stream_build(flow_id: str):
return
logger.debug("Building langchain object")
graph = Graph.from_payload(graph_data)
try:
# Some error could happen when building the graph
graph = Graph.from_payload(graph_data)
except Exception as exc:
logger.error(exc)
error_message = str(exc)
yield f"error: {json.dumps({'error': error_message})}\n\n"
return
number_of_nodes = len(graph.nodes)
for i, vertex in enumerate(graph.generator_build(), 1):
try: