Skip to content

fix: use null instead of empty string for assistant tool-call messages#1094

Open
octo-patch wants to merge 1 commit into
ItzCrazyKns:masterfrom
octo-patch:fix/issue-1080-null-assistant-content
Open

fix: use null instead of empty string for assistant tool-call messages#1094
octo-patch wants to merge 1 commit into
ItzCrazyKns:masterfrom
octo-patch:fix/issue-1080-null-assistant-content

Conversation

@octo-patch

@octo-patch octo-patch commented Apr 6, 2026

Copy link
Copy Markdown

Fixes #1080

Problem

When the researcher agent completes an iteration and pushes an assistant message with tool_calls back into the conversation history, the content field was set to an empty string (''). This violates the OpenAI API specification, which requires content to be null (not '') when tool_calls are present.

Several OpenAI-compatible providers such as OpenRouter strictly enforce this rule and reject assistant messages with empty string content, throwing errors like Error: is empty. This caused all API calls via /api/search to fail with a 500 error for users of such providers.

Solution

  • Changed AssistantMessage.content type from string to string | null in src/lib/types.ts
  • Changed content: '' to content: null in researcher/index.ts for the assistant message pushed when tool calls are present
  • Updated formatHistory.ts to guard against null content with a ?? '' fallback

Testing

  • Confirmed the convertToOpenAIMessages function in openaiLLM.ts already accepts null content (it maps directly to ChatCompletionAssistantMessageParam which types content as string | null | ...)
  • The fix aligns with the OpenAI API spec: when tool_calls is present, content should be null

Summary by cubic

Set assistant message content to null when tool_calls are present to match the OpenAI spec and stop 500s from strict providers like OpenRouter. Fixes #1080.

  • Bug Fixes
    • Changed AssistantMessage.content type to string | null.
    • Researcher now pushes assistant messages with content: null instead of '' when tool_calls exist.
    • Added ?? '' fallback in history formatter to handle null content.

Written for commit d28986d. Summary will update on new commits.

When the researcher agent pushes an assistant message with tool_calls
back into the conversation history, the content field was set to an
empty string which violates the OpenAI API specification. The spec
requires content to be null when tool_calls are present.

Several OpenAI-compatible providers such as OpenRouter strictly validate
this and throw errors like 'Error: is empty', causing all API requests
to fail with a 500 error.

Fixes ItzCrazyKns#1080

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

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.

Cant use perplexica API (Error: is empty)

1 participant