Parsing NDJSON stream
Some API endpoints return the response as a NDJSON stream. This page describes how to parse them.
For example, the endpoint Agent Graph Invoke returns a stream of sequential JSON objects. If you try to parse the raw response as a JSON, it will fail:
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:
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)
Last updated