fix: log the exception of graph execution (#577)

* fix: log the exeption of graph execution

* Update src/server/app.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Willem Jiang 2025-09-14 21:20:25 +08:00 committed by GitHub
parent a265ef47ff
commit dac0afbca4
1 changed files with 28 additions and 17 deletions

View File

@ -4,7 +4,7 @@
import base64 import base64
import json import json
import logging import logging
from typing import Annotated, List, cast from typing import Annotated, Any, List, cast
from uuid import uuid4 from uuid import uuid4
from fastapi import FastAPI, HTTPException, Query from fastapi import FastAPI, HTTPException, Query
@ -244,6 +244,7 @@ async def _stream_graph_events(
graph_instance, workflow_input, workflow_config, thread_id graph_instance, workflow_input, workflow_config, thread_id
): ):
"""Stream events from the graph and process them.""" """Stream events from the graph and process them."""
try:
async for agent, _, event_data in graph_instance.astream( async for agent, _, event_data in graph_instance.astream(
workflow_input, workflow_input,
config=workflow_config, config=workflow_config,
@ -256,13 +257,23 @@ async def _stream_graph_events(
continue continue
message_chunk, message_metadata = cast( message_chunk, message_metadata = cast(
tuple[BaseMessage, dict[str, any]], event_data tuple[BaseMessage, dict[str, Any]], event_data
) )
async for event in _process_message_chunk( async for event in _process_message_chunk(
message_chunk, message_metadata, thread_id, agent message_chunk, message_metadata, thread_id, agent
): ):
yield event yield event
except Exception as e:
logger.exception("Error during graph execution")
yield _make_event(
"error",
{
"thread_id": thread_id,
"error": str(e),
},
)
async def _astream_workflow_generator( async def _astream_workflow_generator(