# How to get structured output when using astream() or stream()

**URL:** <https://forum.langchain.com/t/how-to-get-structured-output-when-using-astream-or-stream/4509>\
**Category:** LangChain\
**Tags:** python-help\
**Created:** [September 8, 2026, 2:13am UTC](https://forum.langchain.com/t/how-to-get-structured-output-when-using-astream-or-stream/4509 "2026-09-08T02:13:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MSJavaScript](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/msjavascript/32/3069_2.png) [@MSJavaScript](https://forum.langchain.com/u/MSJavaScript)\
**Post date:** [September 8, 2026, 2:13am UTC](https://forum.langchain.com/t/how-to-get-structured-output-when-using-astream-or-stream/4509/1 "2026-09-08T02:13:19Z")

</div>

Hello, I am using `agent.astream()` to print streaming message, but I don’t know how to get structured output like `agent.invoke()`. Anyone has some ideas ?

```python

def print_msg(msg):
    if isinstance(msg, AIMessage):
        print(f"[AIMessage] content: {msg.content}")
        if msg.tool_calls:
            for tc in msg.tool_calls:
                print(f" → tool call: {tc['name']}({tc['args']}) id={tc['id']}")
    elif isinstance(msg, ToolMessage):
        print(f"[ToolMessage] name={msg.name} content: {msg.content[:300]}...")
    elif isinstance(msg, HumanMessage):
        print(f"[Human] {msg.content}")
    else:
        print(f"[{type(msg). __name__ }] {msg}")

async def run_agent(msg:str,
                    provider_id:str,
                    job_env_name:str):
    thread_config = {"configurable": {"thread_id": "12345"}}
    structured_response = None
    async for chunk in agent.astream({"messages": [HumanMessage(msg)]}, 
                            stream_mode="updates", 
                            config=thread_config,
                            context=...,
                            version="v2"):
        for node_name, update in chunk.items():
            print(f"\n===== Node: {node_name} =====")
            if "messages" in update:
                for msg in update["messages"]:
                    print_msg(msg) 

            # does not work
            #if "structured_response" in chunk and chunk["structured_response"]:
            # structured_response = chunk["structured_response"]

```

---

<div class="post-metadata">

**Author:** ![keenborder786](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/keenborder786/32/1757_2.png) [@keenborder786](https://forum.langchain.com/u/keenborder786)\
**Post date:** [September 8, 2026, 11:06am UTC](https://forum.langchain.com/t/how-to-get-structured-output-when-using-astream-or-stream/4509/2 "2026-09-08T11:06:35Z")

</div>

Hello @MSJavaScript ,

`structured_response` lives **inside the node update** , not on the chunk. With `version="v2"` the chunk is `{type, ns, data}`, so `"structured_response" in chunk` is always false.

```python
structured_response = None
async for chunk in agent.astream(..., stream_mode="updates", version="v2"):
    if chunk["type"] != "updates":
        continue
    for node_name, update in chunk["data"].items():
        if "messages" in update:
            for m in update["messages"]:
                print_msg(m)
        if update.get("structured_response") is not None:
            structured_response = update["structured_response"]

```

Or stream full state and take the last snapshot: `stream_mode="values"` → `chunk["data"]["structured_response"]` (same as `invoke()`). Requires `response_format` on `create_agent`.

## Langchain Docs Reference

- [Structured output](https://docs.langchain.com/oss/python/langchain/structured-output)
- [Streaming](https://docs.langchain.com/oss/python/langchain/streaming)
