Skip to content

fix: handle None text in message content sanitization#1920

Open
yoppi wants to merge 1 commit intostrands-agents:mainfrom
yoppi:fix/sanitize-messages-none-text
Open

fix: handle None text in message content sanitization#1920
yoppi wants to merge 1 commit intostrands-agents:mainfrom
yoppi:fix/sanitize-messages-none-text

Conversation

@yoppi
Copy link

@yoppi yoppi commented Mar 17, 2026

Description

_normalize_messages and remove_blank_messages_content_text call .strip() on item["text"] without guarding against None. When a model provider (e.g. Gemini) returns a content block with {"text": None}, this causes TypeError: expected string or bytes-like object, got 'NoneType'.

Add explicit None checks before calling .strip():

  • With tool_use: None text items are removed (same as blank text)
  • Without tool_use: None text is replaced with "[blank text]" (same as blank)

Related Issues

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

`_normalize_messages` and `remove_blank_messages_content_text` call
`.strip()` on `item["text"]` without guarding against `None`. When a
model provider (e.g. Gemini) returns a content block with `{"text": None}`,
this causes `TypeError: expected string or bytes-like object, got 'NoneType'`.

Add explicit `None` checks before calling `.strip()`:
- With tool_use: None text items are removed (same as blank text)
- Without tool_use: None text is replaced with "[blank text]" (same as blank)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant