Skip to content

The Open Standard for Generative UI

Add streaming support to Python stream package (#92)

Rabi Shanker Guhafe860fb8b8fb
diff --git a/py/stream/poetry.lock b/py/stream/poetry.lock
index 33153e39d..15fc70696 100644
--- a/py/stream/poetry.lock
+++ b/py/stream/poetry.lock
@@ -11,6 +11,68 @@ files = [
{file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"},
]
+[[package]]
+name = "colorama"
+version = "0.4.6"
+description = "Cross-platform colored terminal text."
+optional = false
+python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
+files = [
+ {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
+ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
+]
+
+[[package]]
+name = "exceptiongroup"
+version = "1.2.2"
+description = "Backport of PEP 654 (exception groups)"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "exceptiongroup-1.2.2-py3-none-any.whl", hash = "sha256:3111b9d131c238bec2f8f516e123e14ba243563fb135d3fe885990585aa7795b"},
+ {file = "exceptiongroup-1.2.2.tar.gz", hash = "sha256:47c2edf7c6738fafb49fd34290706d1a1a2f4d1c6df275526b62cbb4aa5393cc"},
+]
+
+[package.extras]
+test = ["pytest (>=6)"]
+
+[[package]]
+name = "iniconfig"
+version = "2.0.0"
+description = "brain-dead simple config-ini parsing"
+optional = false
+python-versions = ">=3.7"
+files = [
+ {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},
+ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},
+]
+
+[[package]]
+name = "packaging"
+version = "24.2"
+description = "Core utilities for Python packages"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "packaging-24.2-py3-none-any.whl", hash = "sha256:09abb1bccd265c01f4a3aa3f7a7db064b36514d2cba19a2f694fe6150451a759"},
+ {file = "packaging-24.2.tar.gz", hash = "sha256:c228a6dc5e932d346bc5739379109d49e8853dd8223571c7c5b55260edc0b97f"},
+]
+
+[[package]]
+name = "pluggy"
+version = "1.5.0"
+description = "plugin and hook calling mechanisms for python"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "pluggy-1.5.0-py3-none-any.whl", hash = "sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669"},
+ {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"},
+]
+
+[package.extras]
+dev = ["pre-commit", "tox"]
+testing = ["pytest", "pytest-benchmark"]
+
[[package]]
name = "pydantic"
version = "2.10.6"
@@ -143,15 +205,37 @@ files = [
[package.dependencies]
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
+[[package]]
+name = "pytest"
+version = "8.3.5"
+description = "pytest: simple powerful testing with Python"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "pytest-8.3.5-py3-none-any.whl", hash = "sha256:c69214aa47deac29fad6c2a4f590b9c4a9fdb16a403176fe154b79c0b4d4d820"},
+ {file = "pytest-8.3.5.tar.gz", hash = "sha256:f4efe70cc14e511565ac476b57c279e12a855b11f48f212af1080ef2263d3845"},
+]
+
+[package.dependencies]
+colorama = {version = "*", markers = "sys_platform == \"win32\""}
+exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}
+iniconfig = "*"
+packaging = "*"
+pluggy = ">=1.5,<2"
+tomli = {version = ">=1", markers = "python_version < \"3.11\""}
+
+[package.extras]
+dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
+
[[package]]
name = "setuptools"
-version = "76.0.0"
+version = "76.1.0"
description = "Easily download, build, install, upgrade, and uninstall Python packages"
optional = false
python-versions = ">=3.9"
files = [
- {file = "setuptools-76.0.0-py3-none-any.whl", hash = "sha256:199466a166ff664970d0ee145839f5582cb9bca7a0a3a2e795b6a9cb2308e9c6"},
- {file = "setuptools-76.0.0.tar.gz", hash = "sha256:43b4ee60e10b0d0ee98ad11918e114c70701bc6051662a9a675a0496c1a158f4"},
+ {file = "setuptools-76.1.0-py3-none-any.whl", hash = "sha256:34750dcb17d046929f545dec9b8349fe42bf4ba13ddffee78428aec422dbfb73"},
+ {file = "setuptools-76.1.0.tar.gz", hash = "sha256:4959b9ad482ada2ba2320c8f1a8d8481d4d8d668908a7a1b84d987375cd7f5bd"},
]
[package.extras]
@@ -163,6 +247,47 @@ enabler = ["pytest-enabler (>=2.2)"]
test = ["build[virtualenv] (>=1.0.3)", "filelock (>=3.4.0)", "ini2toml[lite] (>=0.14)", "jaraco.develop (>=7.21)", "jaraco.envs (>=2.2)", "jaraco.path (>=3.7.2)", "jaraco.test (>=5.5)", "packaging (>=24.2)", "pip (>=19.1)", "pyproject-hooks (!=1.1)", "pytest (>=6,!=8.1.*)", "pytest-home (>=0.5)", "pytest-perf", "pytest-subprocess", "pytest-timeout", "pytest-xdist (>=3)", "tomli-w (>=1.0.0)", "virtualenv (>=13.0.0)", "wheel (>=0.44.0)"]
type = ["importlib_metadata (>=7.0.2)", "jaraco.develop (>=7.21)", "mypy (==1.14.*)", "pytest-mypy"]
+[[package]]
+name = "tomli"
+version = "2.2.1"
+description = "A lil' TOML parser"
+optional = false
+python-versions = ">=3.8"
+files = [
+ {file = "tomli-2.2.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:678e4fa69e4575eb77d103de3df8a895e1591b48e740211bd1067378c69e8249"},
+ {file = "tomli-2.2.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:023aa114dd824ade0100497eb2318602af309e5a55595f76b626d6d9f3b7b0a6"},
+ {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ece47d672db52ac607a3d9599a9d48dcb2f2f735c6c2d1f34130085bb12b112a"},
+ {file = "tomli-2.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6972ca9c9cc9f0acaa56a8ca1ff51e7af152a9f87fb64623e31d5c83700080ee"},
+ {file = "tomli-2.2.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:c954d2250168d28797dd4e3ac5cf812a406cd5a92674ee4c8f123c889786aa8e"},
+ {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:8dd28b3e155b80f4d54beb40a441d366adcfe740969820caf156c019fb5c7ec4"},
+ {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:e59e304978767a54663af13c07b3d1af22ddee3bb2fb0618ca1593e4f593a106"},
+ {file = "tomli-2.2.1-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:33580bccab0338d00994d7f16f4c4ec25b776af3ffaac1ed74e0b3fc95e885a8"},
+ {file = "tomli-2.2.1-cp311-cp311-win32.whl", hash = "sha256:465af0e0875402f1d226519c9904f37254b3045fc5084697cefb9bdde1ff99ff"},
+ {file = "tomli-2.2.1-cp311-cp311-win_amd64.whl", hash = "sha256:2d0f2fdd22b02c6d81637a3c95f8cd77f995846af7414c5c4b8d0545afa1bc4b"},
+ {file = "tomli-2.2.1-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:4a8f6e44de52d5e6c657c9fe83b562f5f4256d8ebbfe4ff922c495620a7f6cea"},
+ {file = "tomli-2.2.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:8d57ca8095a641b8237d5b079147646153d22552f1c637fd3ba7f4b0b29167a8"},
+ {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4e340144ad7ae1533cb897d406382b4b6fede8890a03738ff1683af800d54192"},
+ {file = "tomli-2.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:db2b95f9de79181805df90bedc5a5ab4c165e6ec3fe99f970d0e302f384ad222"},
+ {file = "tomli-2.2.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:40741994320b232529c802f8bc86da4e1aa9f413db394617b9a256ae0f9a7f77"},
+ {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:400e720fe168c0f8521520190686ef8ef033fb19fc493da09779e592861b78c6"},
+ {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:02abe224de6ae62c19f090f68da4e27b10af2b93213d36cf44e6e1c5abd19fdd"},
+ {file = "tomli-2.2.1-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:b82ebccc8c8a36f2094e969560a1b836758481f3dc360ce9a3277c65f374285e"},
+ {file = "tomli-2.2.1-cp312-cp312-win32.whl", hash = "sha256:889f80ef92701b9dbb224e49ec87c645ce5df3fa2cc548664eb8a25e03127a98"},
+ {file = "tomli-2.2.1-cp312-cp312-win_amd64.whl", hash = "sha256:7fc04e92e1d624a4a63c76474610238576942d6b8950a2d7f908a340494e67e4"},
+ {file = "tomli-2.2.1-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f4039b9cbc3048b2416cc57ab3bda989a6fcf9b36cf8937f01a6e731b64f80d7"},
+ {file = "tomli-2.2.1-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:286f0ca2ffeeb5b9bd4fcc8d6c330534323ec51b2f52da063b11c502da16f30c"},
+ {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a92ef1a44547e894e2a17d24e7557a5e85a9e1d0048b0b5e7541f76c5032cb13"},
+ {file = "tomli-2.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9316dc65bed1684c9a98ee68759ceaed29d229e985297003e494aa825ebb0281"},
+ {file = "tomli-2.2.1-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:e85e99945e688e32d5a35c1ff38ed0b3f41f43fad8df0bdf79f72b2ba7bc5272"},
+ {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:ac065718db92ca818f8d6141b5f66369833d4a80a9d74435a268c52bdfa73140"},
+ {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:d920f33822747519673ee656a4b6ac33e382eca9d331c87770faa3eef562aeb2"},
+ {file = "tomli-2.2.1-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:a198f10c4d1b1375d7687bc25294306e551bf1abfa4eace6650070a5c1ae2744"},
+ {file = "tomli-2.2.1-cp313-cp313-win32.whl", hash = "sha256:d3f5614314d758649ab2ab3a62d4f2004c825922f9e370b29416484086b264ec"},
+ {file = "tomli-2.2.1-cp313-cp313-win_amd64.whl", hash = "sha256:a38aa0308e754b0e3c67e344754dff64999ff9b513e691d0e786265c93583c69"},
+ {file = "tomli-2.2.1-py3-none-any.whl", hash = "sha256:cb55c73c5f4408779d0cf3eef9f762b9c9f147a77de7b258bef0a5628adc85cc"},
+ {file = "tomli-2.2.1.tar.gz", hash = "sha256:cd45e1dc79c835ce60f7404ec8119f2eb06d38b1deba146f07ced3bbc44505ff"},
+]
+
[[package]]
name = "types-setuptools"
version = "76.0.0.20250313"
@@ -191,4 +316,4 @@ files = [
[metadata]
lock-version = "2.0"
python-versions = ">=3.9"
-content-hash = "6dbb4841929e2cd379b2c804ab6a261413788d70cedd18a495e3d62c7105a2f2"
+content-hash = "25a3b85695b75aa4005a08f6c381fbe48a865e4a1231362c5396f71981a94703"
diff --git a/py/stream/pyproject.toml b/py/stream/pyproject.toml
index 16041d039..e9dffb9dd 100644
--- a/py/stream/pyproject.toml
+++ b/py/stream/pyproject.toml
@@ -18,6 +18,9 @@ typing-extensions = ">=4.0.0"
types-setuptools = "^76.0.0.20250313"
+[tool.poetry.group.dev.dependencies]
+pytest = "^8.3.5"
+
[tool.black]
line-length = 88
target-version = ["py39"]
diff --git a/py/stream/setup.py b/py/stream/setup.py
index 15a99697e..41d2687cf 100644
--- a/py/stream/setup.py
+++ b/py/stream/setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="crayonai_stream",
- version="0.6.0",
+ version="0.6.1",
description="Streaming utilities for CrayonAI",
author="CrayonAI",
packages=find_packages(where="src"),
diff --git a/py/stream/src/crayonai_stream/__init__.py b/py/stream/src/crayonai_stream/__init__.py
index 7fbf876cd..7c8be9f3a 100644
--- a/py/stream/src/crayonai_stream/__init__.py
+++ b/py/stream/src/crayonai_stream/__init__.py
@@ -4,7 +4,7 @@ from crayonai_stream.js_schema_helpers import (
pydantic_to_template_schema,
templates_to_response_format,
)
-from crayonai_stream.openai_helper import toOpenAIMessage
+from crayonai_stream.logger import setup_logging
from crayonai_stream.protocol import (
Chunk,
ContextUpdate,
@@ -29,8 +29,8 @@ __all__ = [
"pydantic_to_template_schema",
"templates_to_response_format",
"TextResponseSchema",
- "toOpenAIMessage",
- "crayon_stream",
"CrayonMessage",
"TemplateDefinition",
+ "crayon_stream",
+ "setup_logging",
]
diff --git a/py/stream/src/crayonai_stream/best_effort_json_parser.py b/py/stream/src/crayonai_stream/best_effort_json_parser.py
new file mode 100644
index 000000000..c071ac60d
--- /dev/null
+++ b/py/stream/src/crayonai_stream/best_effort_json_parser.py
@@ -0,0 +1,71 @@
+from typing import Any, Optional
+
+from crayonai_stream.logger import logger
+
+
+def invariant(condition: bool, message: Optional[str] = None) -> None:
+ if not condition:
+ raise ValueError(message or "invalid json")
+
+
+def parse(text: str) -> Any:
+ """
+ Parse a prefix of a valid JSON string into a dictionary.
+
+ This function assumes the input is always a prefix of a valid JSON object.
+ It will complete any unfinished JSON structure by adding missing closing braces
+ and brackets.
+
+ Args:
+ text (str): A string that is a prefix of a valid JSON object
+
+ Returns:
+ dict: The parsed JSON object
+
+ Example:
+ >>> parse('{"key": ["val')
+ {'key': ['val']}
+ """
+ if not text or text.isspace():
+ return {}
+
+ # Track both braces and brackets
+ stack = []
+ in_string = False
+ escape_next = False
+
+ for char in text:
+ if char == "\\":
+ escape_next = not escape_next
+ continue
+
+ if char == '"' and not escape_next:
+ in_string = not in_string
+ elif not in_string:
+ if char in "{[":
+ stack.append(char)
+ elif char == "}":
+ invariant(stack[-1] == "{")
+ stack.pop()
+ elif char == "]":
+ invariant(stack[-1] == "[")
+ stack.pop()
+
+ escape_next = False
+
+ # Complete the JSON by adding missing closing quotes and braces/brackets
+ completed_json = text
+ if in_string:
+ completed_json += '"'
+
+ for char in reversed(stack):
+ if char == "{":
+ completed_json += "}"
+ elif char == "[":
+ completed_json += "]"
+
+ import json
+
+ js = json.loads(completed_json)
+ logger.debug(f"parsed: {js}")
+ return js
diff --git a/py/stream/src/crayonai_stream/crayon_message.py b/py/stream/src/crayonai_stream/crayon_message.py
index 44e2617f5..379d0c8ca 100644
--- a/py/stream/src/crayonai_stream/crayon_message.py
+++ b/py/stream/src/crayonai_stream/crayon_message.py
@@ -1,6 +1,6 @@
from typing import List, Literal, Union
-from pydantic import BaseModel
+from pydantic import BaseModel, ConfigDict
class CrayonMessage(BaseModel):
@@ -8,5 +8,4 @@ class CrayonMessage(BaseModel):
role: Literal["user", "assistant"]
message: Union[str, List[dict]]
- class Config:
- extra = "allow" # Allow extra fields
+ model_config = ConfigDict(extra="allow") # Allow extra fields
diff --git a/py/stream/src/crayonai_stream/integrations/__init__.py b/py/stream/src/crayonai_stream/integrations/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/py/stream/src/crayonai_stream/integrations/openai/__init__.py b/py/stream/src/crayonai_stream/integrations/openai/__init__.py
new file mode 100644
index 000000000..e980cbfd1
--- /dev/null
+++ b/py/stream/src/crayonai_stream/integrations/openai/__init__.py
@@ -0,0 +1,7 @@
+from crayonai_stream.integrations.openai.helper import toOpenAIMessage
+from crayonai_stream.integrations.openai.stream import openai_crayon_stream
+
+__all__ = [
+ "toOpenAIMessage",
+ "openai_crayon_stream",
+]
diff --git a/py/stream/src/crayonai_stream/openai_helper.py b/py/stream/src/crayonai_stream/integrations/openai/helper.py
similarity index 90%
rename from py/stream/src/crayonai_stream/openai_helper.py
rename to py/stream/src/crayonai_stream/integrations/openai/helper.py
index 9b063a5a4..4d027d6ab 100644
--- a/py/stream/src/crayonai_stream/openai_helper.py
+++ b/py/stream/src/crayonai_stream/integrations/openai/helper.py
@@ -3,10 +3,8 @@ from typing import Union
from openai.types.chat import (
ChatCompletionAssistantMessageParam,
- ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
)
-from pydantic import BaseModel
from crayonai_stream.crayon_message import CrayonMessage
@@ -25,7 +23,6 @@ def toOpenAIMessage(
) -> Union[
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
- ChatCompletionSystemMessageParam,
]:
if msg.role == "user":
return ChatCompletionUserMessageParam(
diff --git a/py/stream/src/crayonai_stream/integrations/openai/stream.py b/py/stream/src/crayonai_stream/integrations/openai/stream.py
new file mode 100644
index 000000000..d74c7cda0
--- /dev/null
+++ b/py/stream/src/crayonai_stream/integrations/openai/stream.py
@@ -0,0 +1,23 @@
+import json
+from typing import Iterator
+
+from openai import Stream
+from openai._streaming import Stream
+from openai.types.chat import ChatCompletionChunk
+
+from crayonai_stream import crayon_stream
+
+
+def _openai_crayon_stream(
+ stream: Stream[ChatCompletionChunk],
+) -> Iterator[str]:
+ for chunk in stream:
+ text = chunk.choices[0].delta.content
+ if text is not None:
+ yield text
+
+
+def openai_crayon_stream(
+ stream: Stream[ChatCompletionChunk],
+) -> Iterator[str]:
+ return crayon_stream(_openai_crayon_stream(stream))
diff --git a/py/stream/src/crayonai_stream/logger.py b/py/stream/src/crayonai_stream/logger.py
new file mode 100644
index 000000000..ac9edfcbd
--- /dev/null
+++ b/py/stream/src/crayonai_stream/logger.py
@@ -0,0 +1,27 @@
+import logging
+
+# Create a logger for your library
+logger = logging.getLogger("crayonai_stream")
+# By default, don't output any logs
+logger.addHandler(logging.NullHandler())
+
+
+def setup_logging(level: int = logging.INFO) -> None:
+ """
+ Configure logging for the library.
+ Users can call this function to enable logging with their desired level.
+
+ Args:
+ level: The logging level (e.g., logging.INFO, logging.DEBUG, etc.)
+ """
+ # Remove existing handlers to avoid duplicates
+ for handler in logger.handlers[:]:
+ logger.removeHandler(handler)
+
+ handler = logging.StreamHandler()
+ formatter = logging.Formatter(
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
+ )
+ handler.setFormatter(formatter)
+ logger.addHandler(handler)
+ logger.setLevel(level)
diff --git a/py/stream/src/crayonai_stream/stream.py b/py/stream/src/crayonai_stream/stream.py
index 71573740e..9de94eabc 100644
--- a/py/stream/src/crayonai_stream/stream.py
+++ b/py/stream/src/crayonai_stream/stream.py
@@ -1,30 +1,85 @@
-import json
-from typing import Iterator
+from typing import Iterator, Union
-from openai import Stream
-from openai._streaming import Stream
-from openai.types.chat import ChatCompletionChunk
+from crayonai_stream import ResponseTemplate, TextChunk
+from crayonai_stream.best_effort_json_parser import parse
+from crayonai_stream.logger import logger
-from crayonai_stream import Error, ResponseTemplate, TextChunk
-
-# TODO: Implement actual streaming.
def crayon_stream(
- stream: Stream[ChatCompletionChunk],
+ stream: Iterator[str],
) -> Iterator[str]:
streamed_text = ""
- try:
- for chunk in stream:
- text = chunk.choices[0].delta.content
- if text is not None:
- streamed_text += text
- parsed = json.loads(streamed_text)
- for item in parsed["response"]:
- if item["type"] == "text":
- yield TextChunk(chunk=item["text"]).toSSEString()
- elif item["type"] == "template":
- yield ResponseTemplate(
- name=item["name"], templateProps=item["templateProps"]
+ previously_streamed_text = ""
+ has_pending_template = False
+
+ def finish() -> Union[str, None]:
+ nonlocal streamed_text, has_pending_template
+ parsed = parse(streamed_text)
+ response_list = parsed.get("response", [])
+ last_content = response_list[-1] if response_list else None
+
+ if has_pending_template and last_content and last_content.get("type") != "text":
+ name = last_content.get("name")
+ if not name:
+ logger.error("name is required in ResponseTemplate")
+ raise ValueError("name is required in ResponseTemplate")
+ template_props = last_content.get("templateProps", {})
+ return ResponseTemplate(
+ name=name, templateProps=template_props
+ ).toSSEString()
+ return None
+
+ for text in stream:
+ logger.debug(f"recv: {text}")
+ streamed_text += text
+ try:
+ parsed = parse(streamed_text)
+ except Exception:
+ # In case best-effort JSON parser fails, we just ignore the error
+ # and continue to parse the next chunk.
+ logger.debug("Failed to parse partial response, ignoring")
+ continue
+ response_list = parsed.get("response", [])
+ new_content = response_list[-1] if response_list else None
+
+ if not new_content:
+ continue
+
+ if new_content.get("type") == "text":
+ remaining_responses = response_list[:-1]
+ prev_content = remaining_responses[-1] if remaining_responses else None
+ new_text = new_content.get("text", "")
+
+ if (
+ has_pending_template
+ and prev_content
+ and prev_content.get("type") != "text"
+ ):
+ name = prev_content.get("name")
+ if not name:
+ logger.error("name is required in ResponseTemplate")
+ raise ValueError("name is required in ResponseTemplate")
+ template_props = prev_content.get("templateProps", {})
+ template = ResponseTemplate(
+ name=name, templateProps=template_props
).toSSEString()
- except Exception as e:
- yield Error(error=str(e)).toSSEString()
+ logger.debug(f"Sending template: {template}")
+ yield template
+ has_pending_template = False
+
+ text_content = new_text[len(previously_streamed_text) :]
+
+ if text_content:
+ logger.debug(f"Sending text: {text_content}")
+ yield TextChunk(chunk=text_content).toSSEString()
+
+ previously_streamed_text = new_text
+ else:
+ has_pending_template = True
+ previously_streamed_text = ""
+
+ # After the stream is finished, we need to finalize the template if it's pending
+ final_template = finish()
+ if final_template:
+ logger.debug(f"Finalizing template: {final_template}")
+ yield final_template
diff --git a/py/stream/tests/test_best_effort_json_parser.py b/py/stream/tests/test_best_effort_json_parser.py
new file mode 100644
index 000000000..76fe668db
--- /dev/null
+++ b/py/stream/tests/test_best_effort_json_parser.py
@@ -0,0 +1,131 @@
+from crayonai_stream.best_effort_json_parser import parse
+
+
+def test_empty_input():
+ """Test empty and whitespace inputs"""
+ assert parse("") == {}
+ assert parse(" ") == {}
+ assert parse("\n\t") == {}
+
+
+def test_complete_json():
+ """Test already complete JSON objects"""
+ assert parse('{"a": 1}') == {"a": 1}
+ assert parse('{"a": "b"}') == {"a": "b"}
+ assert parse('{"a": true, "b": false}') == {"a": True, "b": False}
+
+
+def test_incomplete_simple():
+ """Test incomplete simple JSON objects"""
+ assert parse('{"a": 1') == {"a": 1}
+ assert parse('{"a": "b"') == {"a": "b"}
+ assert parse('{"a": true') == {"a": True}
+
+
+def test_nested_objects():
+ """Test nested object structures"""
+ assert parse('{"a": {"b": 1') == {"a": {"b": 1}}
+ assert parse('{"a": {"b": {"c": true') == {"a": {"b": {"c": True}}}
+ assert parse('{"a": {"b": 1}, "c": {"d": "text"') == {
+ "a": {"b": 1},
+ "c": {"d": "text"},
+ }
+
+
+def test_arrays():
+ """Test arrays in JSON"""
+ assert parse('{"a": [1, 2, 3]}') == {"a": [1, 2, 3]}
+ assert parse('{"a": [1, 2, 3') == {"a": [1, 2, 3]}
+ assert parse('{"a": [{"b": 1}, {"c": 2}') == {"a": [{"b": 1}, {"c": 2}]}
+ # Test incomplete array with string value
+ assert parse('{"key": ["val') == {"key": ["val"]}
+ # Test the same case again to ensure consistency
+ assert parse('{"key": ["val') == {"key": ["val"]}
+
+
+def test_complex_structures():
+ """Test complex nested structures"""
+ assert parse('{"a": {"b": [1, 2, {"c": true') == {"a": {"b": [1, 2, {"c": True}]}}
+ assert parse('{"data": {"users": [{"name": "John", "active": true') == {
+ "data": {"users": [{"name": "John", "active": True}]}
+ }
+
+
+def test_escaped_quotes():
+ """Test handling of escaped quotes in JSON strings"""
+ # Test complete JSON with escaped quotes
+ assert parse('{"key": "value with \\"quotes\\""}') == {"key": 'value with "quotes"'}
+ # Test incomplete JSON with escaped quotes
+ assert parse('{"key": "value with \\"quotes') == {"key": 'value with "quotes'}
+ # Test incomplete JSON with escaped quotes in array
+ assert parse('{"key": ["value with \\"quotes\\""]') == {
+ "key": ['value with "quotes"']
+ }
+ # Test with escape at the end
+ assert parse('{"key": "value\\\\') == {"key": "value\\"}
+ # Test with escaped quote at the end
+ assert parse('{"key": "value\\"') == {"key": 'value"'}
+
+
+def test_special_numbers():
+ """Test various number formats"""
+ assert parse('{"a": 3.14') == {"a": 3.14}
+ assert parse('{"a": -123') == {"a": -123}
+ assert parse('{"a": 1.23e-4') == {"a": 1.23e-4}
+ assert parse('{"a": [1.23e-4, -456, 3.14') == {"a": [1.23e-4, -456, 3.14]}
+
+
+def test_null_values():
+ """Test null values in different contexts"""
+ assert parse('{"a": null') == {"a": None}
+ assert parse('{"a": [null, 1, null') == {"a": [None, 1, None]}
+ assert parse('{"a": {"b": null') == {"a": {"b": None}}
+
+
+def test_empty_structures():
+ """Test empty arrays and objects"""
+ assert parse('{"a": []') == {"a": []}
+ assert parse('{"a": {}') == {"a": {}}
+ assert parse('{"a": [{}]') == {"a": [{}]}
+ assert parse('{"a": {"b": []') == {"a": {"b": []}}
+
+
+def test_complex_strings():
+ """Test strings containing special characters and structures"""
+ assert parse('{"a": "text with { and } inside"') == {
+ "a": "text with { and } inside"
+ }
+ assert parse('{"a": "text with [ and ] inside"') == {
+ "a": "text with [ and ] inside"
+ }
+ assert parse('{"a": "multiple \\"quoted\\" words"') == {
+ "a": 'multiple "quoted" words'
+ }
+ assert parse('{"a": "\\\\\\""') == {"a": '\\"'} # Testing multiple escapes
+ assert parse('{"a": "line1\\nline2\\tindented"') == {"a": "line1\nline2\tindented"}
+
+
+def test_multiple_incomplete_sections():
+ """Test structures with multiple incomplete sections"""
+ assert parse('{"a": {"b": [{"c": 1}, {"d": "text"}, {"e": [1, 2') == {
+ "a": {"b": [{"c": 1}, {"d": "text"}, {"e": [1, 2]}]}
+ }
+ assert parse('{"a": [{"b": {"c": "text"}, "d": [1, {"e": true') == {
+ "a": [{"b": {"c": "text"}, "d": [1, {"e": True}]}]
+ }
+
+
+def test_unicode_characters():
+ """Test handling of unicode characters"""
+ assert parse('{"a": "emoji πŸŽ‰"') == {"a": "emoji πŸŽ‰"}
+ assert parse('{"a": "unicode ⚑️ chars"') == {"a": "unicode ⚑️ chars"}
+ assert parse('{"a": "mixed unicode 🌟 and \\"quotes\\""') == {
+ "a": 'mixed unicode 🌟 and "quotes"'
+ }
+
+
+# This test doesn't pass because the parser is not handling
+# the case where the key is not closed.
+# def test_partial_key():
+# """Test partial key in JSON"""
+# assert parse('{"a' == {})