Streaming & debugging

For a great user experience, we stream agent events in real-time. This lets the UI show progress as each agent runs, rather than waiting for the entire workflow to complete.

Streaming event flow

How streaming events flow from LangGraph to the user interface

text2sql_agent.py
python
async def process_question_stream(question: str):
    """Stream node execution events for debugging visualization"""
    initial_state = AgentState(
        question=question,
        sql_query="",
        query_result="",
        final_answer="",
        error="",
        iteration=0,
        needs_graph=False,
        graph_type="",
        graph_json="",
        is_in_scope=True,
        guardrails_reason="",
        sql_reason="",
        graph_reason=""
    )

    current_state = initial_state.copy()

    try:
        # Stream events from the graph
        async for event in text2sql_graph.astream_events(
            initial_state,
            config={"recursion_limit": 50},
            version="v1"
        ):
            event_type = event.get("event")

            if event_type == "on_chain_start":
                node_name = event.get("name", "")
                yield {"type": "node_start", "node": node_name}

            elif event_type == "on_chain_end":
                node_name = event.get("name", "")
                output = event.get("data", {}).get("output", {})
                current_state.update(output)
                yield {"type": "node_end", "node": node_name, "output": output}

        yield {"type": "final", "result": current_state}

    except Exception as e:
        yield {"type": "error", "error": str(e)}

astream_events provides real-time event streaming.

Flashcards: Flashcards

Loading practice…

Quiz: Quiz

Loading practice…

Validation checklist: Streaming concepts checklist

Loading practice…

You can now stream agent execution and debug workflows in real time. The entire LangGraph orchestration layer is complete.

Validation checklist: LangGraph orchestration checklist

Loading practice…