> For the complete documentation index, see [llms.txt](https://docs.connectly.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.connectly.ai/parsing-ndjson-stream.md).

# Parsing NDJSON stream

For example, the endpoint [Agent Graph Invoke ](/agent-graph/agent-graph-api/invoke-stream.md)returns a stream of sequential JSON objects. If you try to parse the raw response as a JSON, it will fail:

```python
import requests
response = requests.post("https://api.connectly.ai/external/v1/ai/agent_graph/invoke", ...)
data = response.json()  # <- fails with JSON parse error
```

This happens because internally we stream the events to allow a smoother user experience.

To properly parse the response, you can use this logic:

```python
import requests
import json

url = "https://api.connectly.ai/external/v1/ai/agent_graph/invoke"
payload = json.dumps(
    {
        "clientKey": "***",
        "businessId": "***",
        "sessionId": "b6bed81c-5fda-486d-b96c-1ff3af36219a",
        "inputEvents": [
            {
                "messageEvent": {
                    "role": "USER",
                    "content": {"textContent": {"text": "Hi"}},
                }
            }
        ],
    }
)
headers = {"Content-Type": "application/json"}
response = requests.request("POST", url, headers=headers, data=payload, stream=True)

for raw_line in response.iter_lines():  # <- parse each line as a JSON
    if raw_line:
        line = raw_line.decode("utf-8")
        data = json.loads(line)
        print(data)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.connectly.ai/parsing-ndjson-stream.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
