Describe the Bug
When I following the quickstart guide example for doing a streaming response using the Python SDK, it does not work. The chat completion create is supposed to return a stream that we iterate through in chunks, and each chunk is an object where we extract the delta response (string) and print that. Instead, both the stream and the chunks are strings themselves that are correct representations of what the API returns but are not casted to the correct object and not JSON parsable.
Expected Behavior
The quickstart guide should work.
Actual Behavior
The code example does not work.
Steps to Reproduce
- Call the API with the following request: any message,
stream=True
- Observe the unexpected behavior.
API Request & Response (if applicable)
import os
from perplexity import Perplexity
api_key = os.getenv("PERPLEXITY_API_KEY")
client = Perplexity(api_key=api_key)
stream = client.chat.completions.create(
model="sonar-pro",
messages = [{"role": "user", "content": "What is the capital of France?"}],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content: # <-- breaks here
print(chunk.choices[0].delta.content, end="")
Environment
- API Version: sonar-pro
- SDK (if applicable): Python SDK 0.11.0, Python 3.11.9
- Operating System: [e.g., MacOS, Linux, Windows]
Logs or Screenshots (if applicable)
Sanity check: the API is still returning correct data as strings, so itβs not as issue with the API key or the actual Perplexity API being down.
data: {"id": "1d72f412-1c00-44e2-a0d5-d5b54710676e", "model": "sonar-pro", "created": 1758758428, "usage": {"prompt_tokens": 7, "completion_tokens": 1, "total_tokens": 8, "search_context_size": "low"}, "citations": ["https://home.adelphi.edu/~ca19535/page%204.html", "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "https://www.britannica.com/place/France", "https://en.wikipedia.org/wiki/Paris", "https://www.britannica.com/place/Paris", "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html"], "search_results": [{"title": "Paris facts: the capital of France in history", "url": "https://home.adelphi.edu/~ca19535/page%204.html", "date": "2000-01-01", "last_updated": "2025-09-24", "snippet": "Paris is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011."}, {"title": "Paris 4K - Paris Capital Of France In 4K | - YouTube", "url": "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "date": "2024-12-07", "last_updated": "2025-08-10", "snippet": "Paris is the capital and largest city of France , Paris is the fourth largest city in the European Union and the 30th most densely ..."}, {"title": "France | History, Maps, Flag, Population, Cities, Capital, & Facts", "url": "https://www.britannica.com/place/France", "date": "2025-09-24", "last_updated": "2025-09-24", "snippet": "The capital and by far the most important city of France is Paris, one of the world's preeminent cultural and commercial centres."}, {"title": "Paris - Wikipedia", "url": "https://en.wikipedia.org/wiki/Paris", "date": "2001-11-06", "last_updated": "2025-09-24", "snippet": "Paris is the capital and largest city of France, with an estimated city center population of 2,048,472, and a metropolitan area of 13,171,056 as of January ..."}, {"title": "Paris | Definition, Map, Population, Facts, & History | Britannica", "url": "https://www.britannica.com/place/Paris", "date": "2025-09-22", "last_updated": "2025-09-24", "snippet": "Paris, city and capital of France, located along the Seine River, in the north-central part of the country. Paris is one of the world's most ..."}, {"title": "List of capitals of France - Wikipedia", "url": "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "date": "2003-01-15", "last_updated": "2025-09-03", "snippet": "This is a chronological list of capitals of France. The capital of France has been Paris since its liberation in 1944."}, {"title": "Discover the city of Paris | Paris the capital city of France", "url": "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "date": "2022-12-01", "last_updated": "2025-08-29", "snippet": "Paris is the city of romance par excellence, the fashion capital and the best example of French art de vivre. Exploring Paris is an essential rite of passage ..."}, {"title": "The birth of the french capital - History of Paris city", "url": "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "date": "2006-01-01", "last_updated": "2025-09-19", "snippet": "During this millennium, Paris became the capital of France. This resulted from a long and complicated history of kings, to which each succeeding dynasty ..."}, {"title": "What is the capital of France? Overview of Paris - iRoamly Travel eSIM", "url": "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html", "date": "2024-11-28", "last_updated": "2025-09-24", "snippet": "Dive into the charm of Paris, the capital of France! Explore its rich history, iconic attractions, and delectable cuisine in our guide."}], "object": "chat.completion", "choices": [{"index": 0, "finish_reason": null, "message": {"role": "assistant", "content": "The"}, "delta": {"role": "assistant", "content": "The"}}]}
data: {"id": "1d72f412-1c00-44e2-a0d5-d5b54710676e", "model": "sonar-pro", "created": 1758758428, "usage": {"prompt_tokens": 7, "completion_tokens": 2, "total_tokens": 9, "search_context_size": "low"}, "citations": ["https://home.adelphi.edu/~ca19535/page%204.html", "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "https://www.britannica.com/place/France", "https://en.wikipedia.org/wiki/Paris", "https://www.britannica.com/place/Paris", "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html"], "search_results": [{"title": "Paris facts: the capital of France in history", "url": "https://home.adelphi.edu/~ca19535/page%204.html", "date": "2000-01-01", "last_updated": "2025-09-24", "snippet": "Paris is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011."}, {"title": "Paris 4K - Paris Capital Of France In 4K | - YouTube", "url": "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "date": "2024-12-07", "last_updated": "2025-08-10", "snippet": "Paris is the capital and largest city of France , Paris is the fourth largest city in the European Union and the 30th most densely ..."}, {"title": "France | History, Maps, Flag, Population, Cities, Capital, & Facts", "url": "https://www.britannica.com/place/France", "date": "2025-09-24", "last_updated": "2025-09-24", "snippet": "The capital and by far the most important city of France is Paris, one of the world's preeminent cultural and commercial centres."}, {"title": "Paris - Wikipedia", "url": "https://en.wikipedia.org/wiki/Paris", "date": "2001-11-06", "last_updated": "2025-09-24", "snippet": "Paris is the capital and largest city of France, with an estimated city center population of 2,048,472, and a metropolitan area of 13,171,056 as of January ..."}, {"title": "Paris | Definition, Map, Population, Facts, & History | Britannica", "url": "https://www.britannica.com/place/Paris", "date": "2025-09-22", "last_updated": "2025-09-24", "snippet": "Paris, city and capital of France, located along the Seine River, in the north-central part of the country. Paris is one of the world's most ..."}, {"title": "List of capitals of France - Wikipedia", "url": "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "date": "2003-01-15", "last_updated": "2025-09-03", "snippet": "This is a chronological list of capitals of France. The capital of France has been Paris since its liberation in 1944."}, {"title": "Discover the city of Paris | Paris the capital city of France", "url": "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "date": "2022-12-01", "last_updated": "2025-08-29", "snippet": "Paris is the city of romance par excellence, the fashion capital and the best example of French art de vivre. Exploring Paris is an essential rite of passage ..."}, {"title": "The birth of the french capital - History of Paris city", "url": "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "date": "2006-01-01", "last_updated": "2025-09-19", "snippet": "During this millennium, Paris became the capital of France. This resulted from a long and complicated history of kings, to which each succeeding dynasty ..."}, {"title": "What is the capital of France? Overview of Paris - iRoamly Travel eSIM", "url": "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html", "date": "2024-11-28", "last_updated": "2025-09-24", "snippet": "Dive into the charm of Paris, the capital of France! Explore its rich history, iconic attractions, and delectable cuisine in our guide."}], "object": "chat.completion", "choices": [{"index": 0, "finish_reason": null, "message": {"role": "assistant", "content": "The capital"}, "delta": {"role": "assistant", "content": " capital"}}]}
data: {"id": "1d72f412-1c00-44e2-a0d5-d5b54710676e", "model": "sonar-pro", "created": 1758758428, "usage": {"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10, "search_context_size": "low"}, "citations": ["https://home.adelphi.edu/~ca19535/page%204.html", "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "https://www.britannica.com/place/France", "https://en.wikipedia.org/wiki/Paris", "https://www.britannica.com/place/Paris", "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html"], "search_results": [{"title": "Paris facts: the capital of France in history", "url": "https://home.adelphi.edu/~ca19535/page%204.html", "date": "2000-01-01", "last_updated": "2025-09-24", "snippet": "Paris is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011."}, {"title": "Paris 4K - Paris Capital Of France In 4K | - YouTube", "url": "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "date": "2024-12-07", "last_updated": "2025-08-10", "snippet": "Paris is the capital and largest city of France , Paris is the fourth largest city in the European Union and the 30th most densely ..."}, {"title": "France | History, Maps, Flag, Population, Cities, Capital, & Facts", "url": "https://www.britannica.com/place/France", "date": "2025-09-24", "last_updated": "2025-09-24", "snippet": "The capital and by far the most important city of France is Paris, one of the world's preeminent cultural and commercial centres."}, {"title": "Paris - Wikipedia", "url": "https://en.wikipedia.org/wiki/Paris", "date": "2001-11-06", "last_updated": "2025-09-24", "snippet": "Paris is the capital and largest city of France, with an estimated city center population of 2,048,472, and a metropolitan area of 13,171,056 as of January ..."}, {"title": "Paris | Definition, Map, Population, Facts, & History | Britannica", "url": "https://www.britannica.com/place/Paris", "date": "2025-09-22", "last_updated": "2025-09-24", "snippet": "Paris, city and capital of France, located along the Seine River, in the north-central part of the country. Paris is one of the world's most ..."}, {"title": "List of capitals of France - Wikipedia", "url": "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "date": "2003-01-15", "last_updated": "2025-09-03", "snippet": "This is a chronological list of capitals of France. The capital of France has been Paris since its liberation in 1944."}, {"title": "Discover the city of Paris | Paris the capital city of France", "url": "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "date": "2022-12-01", "last_updated": "2025-08-29", "snippet": "Paris is the city of romance par excellence, the fashion capital and the best example of French art de vivre. Exploring Paris is an essential rite of passage ..."}, {"title": "The birth of the french capital - History of Paris city", "url": "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "date": "2006-01-01", "last_updated": "2025-09-19", "snippet": "During this millennium, Paris became the capital of France. This resulted from a long and complicated history of kings, to which each succeeding dynasty ..."}, {"title": "What is the capital of France? Overview of Paris - iRoamly Travel eSIM", "url": "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html", "date": "2024-11-28", "last_updated": "2025-09-24", "snippet": "Dive into the charm of Paris, the capital of France! Explore its rich history, iconic attractions, and delectable cuisine in our guide."}], "object": "chat.completion", "choices": [{"index": 0, "finish_reason": null, "message": {"role": "assistant", "content": "The capital of"}, "delta": {"role": "assistant", "content": " of"}}]}
data: {"id": "1d72f412-1c00-44e2-a0d5-d5b54710676e", "model": "sonar-pro", "created": 1758758428, "usage": {"prompt_tokens": 7, "completion_tokens": 4, "total_tokens": 11, "search_context_size": "low"}, "citations": ["https://home.adelphi.edu/~ca19535/page%204.html", "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "https://www.britannica.com/place/France", "https://en.wikipedia.org/wiki/Paris", "https://www.britannica.com/place/Paris", "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html"], "search_results": [{"title": "Paris facts: the capital of France in history", "url": "https://home.adelphi.edu/~ca19535/page%204.html", "date": "2000-01-01", "last_updated": "2025-09-24", "snippet": "Paris is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011."}, {"title": "Paris 4K - Paris Capital Of France In 4K | - YouTube", "url": "https://www.youtube.com/watch?v=_6UBxbX4GiQ", "date": "2024-12-07", "last_updated": "2025-08-10", "snippet": "Paris is the capital and largest city of France , Paris is the fourth largest city in the European Union and the 30th most densely ..."}, {"title": "France | History, Maps, Flag, Population, Cities, Capital, & Facts", "url": "https://www.britannica.com/place/France", "date": "2025-09-24", "last_updated": "2025-09-24", "snippet": "The capital and by far the most important city of France is Paris, one of the world's preeminent cultural and commercial centres."}, {"title": "Paris - Wikipedia", "url": "https://en.wikipedia.org/wiki/Paris", "date": "2001-11-06", "last_updated": "2025-09-24", "snippet": "Paris is the capital and largest city of France, with an estimated city center population of 2,048,472, and a metropolitan area of 13,171,056 as of January ..."}, {"title": "Paris | Definition, Map, Population, Facts, & History | Britannica", "url": "https://www.britannica.com/place/Paris", "date": "2025-09-22", "last_updated": "2025-09-24", "snippet": "Paris, city and capital of France, located along the Seine River, in the north-central part of the country. Paris is one of the world's most ..."}, {"title": "List of capitals of France - Wikipedia", "url": "https://en.wikipedia.org/wiki/List_of_capitals_of_France", "date": "2003-01-15", "last_updated": "2025-09-03", "snippet": "This is a chronological list of capitals of France. The capital of France has been Paris since its liberation in 1944."}, {"title": "Discover the city of Paris | Paris the capital city of France", "url": "https://www.cia-france.com/french-kids-teenage-courses/paris-school/visit-paris", "date": "2022-12-01", "last_updated": "2025-08-29", "snippet": "Paris is the city of romance par excellence, the fashion capital and the best example of French art de vivre. Exploring Paris is an essential rite of passage ..."}, {"title": "The birth of the french capital - History of Paris city", "url": "http://www.paris-city.fr/GB/paris-city/au-fil-du-temps/histoire.php", "date": "2006-01-01", "last_updated": "2025-09-19", "snippet": "During this millennium, Paris became the capital of France. This resulted from a long and complicated history of kings, to which each succeeding dynasty ..."}, {"title": "What is the capital of France? Overview of Paris - iRoamly Travel eSIM", "url": "https://www.iroamly.com/france-travel/what-is-the-capital-of-france.html", "date": "2024-11-28", "last_updated": "2025-09-24", "snippet": "Dive into the charm of Paris, the capital of France! Explore its rich history, iconic attractions, and delectable cuisine in our guide."}], "object": "chat.completion", "choices": [{"index": 0, "finish_reason": null, "message": {"role": "assistant", "content": "The capital of France"}, "delta": {"role": "assistant", "content": " France"}}]}
Additional Context
Add any other context about the problem here.