Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(cloudflare): support response_text in streaming chunk parser

Cursor Agentb07908133b82
diff --git a/litellm/llms/cloudflare/chat/transformation.py b/litellm/llms/cloudflare/chat/transformation.py
index d0c2e86f708..aef41079a0a 100644
--- a/litellm/llms/cloudflare/chat/transformation.py
+++ b/litellm/llms/cloudflare/chat/transformation.py
@@ -199,8 +199,10 @@ class CloudflareChatResponseIterator(BaseModelResponseIterator):
index = int(chunk.get("index", 0))
- if "response" in chunk:
+ if "response" in chunk and chunk["response"] is not None:
text = chunk["response"]
+ elif "response_text" in chunk and chunk["response_text"] is not None:
+ text = chunk["response_text"]
returned_chunk = GenericStreamingChunk(
text=text,
diff --git a/tests/llm_translation/test_cloudflare.py b/tests/llm_translation/test_cloudflare.py
index 0c799b4f399..5a6a0008398 100644
--- a/tests/llm_translation/test_cloudflare.py
+++ b/tests/llm_translation/test_cloudflare.py
@@ -43,6 +43,14 @@ def _streaming_chunks() -> list[str]:
]
+def _streaming_chunks_response_text() -> list[str]:
+ return [
+ json.dumps({"response_text": "I am"}),
+ json.dumps({"response_text": " a language"}),
+ json.dumps({"response_text": " model."}),
+ ]
+
+
@pytest.mark.parametrize("sync_mode", [True, False])
def test_completion_cloudflare(sync_mode):
messages = [{"role": "user", "content": "what llm are you"}]
@@ -145,3 +153,76 @@ def test_completion_cloudflare_stream(sync_mode):
if c.choices[0].delta.content
)
assert "language" in content.lower()
+
+
[email protected]("sync_mode", [True, False])
+def test_completion_cloudflare_stream_response_text(sync_mode):
+ """Newer Cloudflare Workers AI models (e.g. Nemotron) emit `response_text`
+ instead of `response` in streamed chunks. The iterator must surface that
+ text so streaming output is not silently empty.
+ """
+ messages = [{"role": "user", "content": "what llm are you"}]
+ raw_chunks = _streaming_chunks_response_text()
+
+ if sync_mode:
+
+ def _iter_lines():
+ for chunk in raw_chunks:
+ yield f"data: {chunk}"
+ yield "data: [DONE]"
+
+ mock_resp = MagicMock()
+ mock_resp.iter_lines.return_value = _iter_lines()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"content-type": "text/event-stream"}
+
+ with patch.object(HTTPHandler, "post", return_value=mock_resp) as mock_post:
+ response = completion(
+ model="cloudflare/@cf/nvidia/nemotron-mini-4b-instruct",
+ messages=messages,
+ max_tokens=15,
+ stream=True,
+ api_base=FAKE_API_BASE,
+ api_key=FAKE_API_KEY,
+ )
+ chunks_received = list(response)
+ mock_post.assert_called_once()
+ else:
+
+ async def _aiter_lines():
+ for chunk in raw_chunks:
+ yield f"data: {chunk}"
+ yield "data: [DONE]"
+
+ mock_resp = MagicMock()
+ mock_resp.aiter_lines.return_value = _aiter_lines()
+ mock_resp.status_code = 200
+ mock_resp.headers = {"content-type": "text/event-stream"}
+
+ async def _run():
+ with patch.object(
+ AsyncHTTPHandler, "post", new_callable=AsyncMock, return_value=mock_resp
+ ) as mock_post:
+ resp = await acompletion(
+ model="cloudflare/@cf/nvidia/nemotron-mini-4b-instruct",
+ messages=messages,
+ max_tokens=15,
+ stream=True,
+ api_base=FAKE_API_BASE,
+ api_key=FAKE_API_KEY,
+ )
+ received = []
+ async for chunk in resp:
+ received.append(chunk)
+ mock_post.assert_called_once()
+ return received
+
+ chunks_received = asyncio.run(_run())
+
+ assert len(chunks_received) > 0
+ content = "".join(
+ c.choices[0].delta.content
+ for c in chunks_received
+ if c.choices[0].delta.content
+ )
+ assert "language" in content.lower()