fix Langfuse SDK: use start_as_current_observation API

This commit is contained in:
2026-04-15 23:34:47 -03:00
parent f03e47d9ba
commit 1b1b1383a1
3 changed files with 56 additions and 36 deletions

View File

@@ -154,22 +154,24 @@ async def trigger_fce(req: FCERequest):
await event_hub.broadcast({"run_id": run_id, **event})
langfuse = _get_langfuse()
trace = langfuse.trace(
name="fce", id=run_id,
metadata={"flight_id": req.flight_id, "scenario": scenario_manager.active_id},
) if langfuse else None
try:
async with connect_servers(["shared", "ops", "passenger"]) as mcp:
result = await run_fce(req.flight_id, mcp, on_event=on_event, trace=trace)
if langfuse:
with langfuse.start_as_current_observation(
name="fce", as_type="agent",
input={"flight_id": req.flight_id},
metadata={"run_id": run_id, "scenario": scenario_manager.active_id},
):
async with connect_servers(["shared", "ops", "passenger"]) as mcp:
result = await run_fce(req.flight_id, mcp, on_event=on_event, langfuse=langfuse)
langfuse.set_current_trace_io(output=result)
else:
async with connect_servers(["shared", "ops", "passenger"]) as mcp:
result = await run_fce(req.flight_id, mcp, on_event=on_event)
runs[run_id] = {"status": "completed", "agent": "fce", "result": result}
if trace:
trace.update(output={"status": "completed", "type": result.get("type", "")})
logger.info("agent_complete agent=fce run_id=%s flight=%s", run_id, req.flight_id)
except Exception as e:
runs[run_id] = {"status": "error", "agent": "fce", "error": str(e)}
if trace:
trace.update(output={"status": "error", "error": str(e)})
logger.error("agent_error agent=fce run_id=%s error=%s", run_id, e)
finally:
if langfuse:
@@ -200,22 +202,24 @@ async def trigger_handover(req: HandoverRequest):
await event_hub.broadcast({"run_id": run_id, **event})
langfuse = _get_langfuse()
trace = langfuse.trace(
name="handover", id=run_id,
metadata={"hubs": req.hubs, "scenario": scenario_manager.active_id},
) if langfuse else None
try:
async with connect_servers(["shared", "ops"]) as mcp:
result = await run_handover(hubs=req.hubs, mcp=mcp, on_event=on_event, trace=trace)
if langfuse:
with langfuse.start_as_current_observation(
name="handover", as_type="agent",
input={"hubs": req.hubs},
metadata={"run_id": run_id, "scenario": scenario_manager.active_id},
):
async with connect_servers(["shared", "ops"]) as mcp:
result = await run_handover(hubs=req.hubs, mcp=mcp, on_event=on_event, langfuse=langfuse)
langfuse.set_current_trace_io(output=result)
else:
async with connect_servers(["shared", "ops"]) as mcp:
result = await run_handover(hubs=req.hubs, mcp=mcp, on_event=on_event)
runs[run_id] = {"status": "completed", "agent": "handover", "result": result}
if trace:
trace.update(output={"status": "completed"})
logger.info("agent_complete agent=handover run_id=%s hubs=%s", run_id, req.hubs)
except Exception as e:
runs[run_id] = {"status": "error", "agent": "handover", "error": str(e)}
if trace:
trace.update(output={"status": "error", "error": str(e)})
logger.error("agent_error agent=handover run_id=%s error=%s", run_id, e)
finally:
if langfuse: