Skip to content

Commit 4a0b4d0

Browse files
cpsievertclaude
andcommitted
fix: preserve whitespace in ContentText for streaming
Move the [empty string] conversion from __init__ to __str__ in ContentText so that whitespace-only text is preserved internally while still displaying nicely. This fixes streaming tests where newlines between words were being converted to [empty string]. Also revert the .isspace() filtering in stream_content() methods since ContentText now preserves the original text. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 7ae41f8 commit 4a0b4d0

File tree

6 files changed

+9
-13
lines changed

6 files changed

+9
-13
lines changed

chatlas/_content.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,9 @@ class ContentText(Content):
168168
text: str
169169
content_type: ContentTypeEnum = "text"
170170

171-
def __init__(self, **data: Any):
172-
super().__init__(**data)
173-
174-
if self.text == "" or self.text.isspace():
175-
self.text = "[empty string]"
176-
177171
def __str__(self):
172+
if self.text == "" or self.text.isspace():
173+
return "[empty string]"
178174
return self.text
179175

180176

chatlas/_provider_anthropic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ def stream_content(self, chunk):
467467
if chunk.type == "content_block_delta":
468468
if chunk.delta.type == "text_delta":
469469
text = chunk.delta.text
470-
if not text:
470+
if text is None:
471471
return None
472472
return ContentText(text=text)
473473
if chunk.delta.type == "thinking_delta":
474474
thinking = chunk.delta.thinking
475-
if not thinking:
475+
if thinking is None:
476476
return None
477477
return ContentThinking(thinking=thinking)
478478
return None

chatlas/_provider_google.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def stream_content(self, chunk):
375375
return None
376376
part = parts[0]
377377
text = part.text
378-
if not text:
378+
if text is None:
379379
return None
380380
# Check if this is thinking content
381381
if getattr(part, "thought", False):

chatlas/_provider_openai.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@ def _chat_perform_args(
295295
def stream_content(self, chunk):
296296
if chunk.type == "response.output_text.delta":
297297
# https://platform.openai.com/docs/api-reference/responses-streaming/response/output_text/delta
298-
if not chunk.delta:
298+
if chunk.delta is None:
299299
return None
300300
return ContentText(text=chunk.delta)
301301
if chunk.type == "response.reasoning_summary_text.delta":
302302
# https://platform.openai.com/docs/api-reference/responses-streaming/response/reasoning_summary_text/delta
303-
if not chunk.delta:
303+
if chunk.delta is None:
304304
return None
305305
return ContentThinking(thinking=chunk.delta)
306306
if chunk.type == "response.reasoning_summary_text.done":

chatlas/_provider_openai_completions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def stream_content(self, chunk):
196196
if not chunk.choices:
197197
return None
198198
text = chunk.choices[0].delta.content
199-
if not text:
199+
if text is None:
200200
return None
201201
return ContentText(text=text)
202202

chatlas/_provider_snowflake.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def stream_content(self, chunk):
363363
if delta is None or "content" not in delta:
364364
return None
365365
text = delta["content"]
366-
if not text:
366+
if text is None:
367367
return None
368368
return ContentText(text=text)
369369

0 commit comments

Comments
 (0)