Skip to content

fix: Skip empty AI messages in output_langchain to prevent API rejection#980

Merged
frdel merged 1 commit intoagent0ai:developmentfrom
hurtdidit:fix/skip-empty-ai-messages
Feb 1, 2026
Merged

fix: Skip empty AI messages in output_langchain to prevent API rejection#980
frdel merged 1 commit intoagent0ai:developmentfrom
hurtdidit:fix/skip-empty-ai-messages

Conversation

@hurtdidit
Copy link
Copy Markdown
Contributor

Problem

Empty or whitespace-only AI messages cause API validation errors with strict providers (OpenAI, Z.ai, GLM):

"Assistant messages must have either content or tool_calls"

Root Cause

This is a companion fix to PR #927 (response.py KeyError fix). When that fix returns an empty string instead of crashing, the empty string can become an empty AIMessage that fails API validation.

The output_langchain() function creates AIMessage objects without validating that content exists.

Solution

  • Pre-compute content before creating message objects
  • Skip AI messages where content is None, empty, or whitespace-only
  • Human messages are not filtered (they can be empty per API spec)

Testing

Deployed and validated on 4 production A0 instances running Z.ai/GLM providers.

Changes

  • python/helpers/history.py: Modified output_langchain() function (lines 519-533)

Empty or whitespace-only AI messages cause API validation errors with strict
providers (OpenAI, Z.ai, GLM):
  "Assistant messages must have either content or tool_calls"

This is a companion fix to PR agent0ai#927 (response.py KeyError fix). When that fix
returns an empty string instead of crashing, the empty string can become an
empty AIMessage that fails API validation.

Changes:
- Pre-compute content before creating message objects
- Skip AI messages where content is None, empty, or whitespace-only
- Human messages are not filtered (they can be empty per spec)

Tested on 4 production A0 instances.
@hurtdidit
Copy link
Copy Markdown
Contributor Author

To add to the above, I found this to be true when using Venice, A0-Venice, Opus, Grok 4.1 Fast. So, definitely not just GLM.

@frdel frdel merged commit 6ca8402 into agent0ai:development Feb 1, 2026
@frdel
Copy link
Copy Markdown
Collaborator

frdel commented Feb 1, 2026

Thank you!

ky3mu4lp pushed a commit to ky3mu4lp/agent-zero that referenced this pull request Feb 24, 2026
…ages

fix: Skip empty AI messages in output_langchain to prevent API rejection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants