Skip to content

πŸ› fix(model-runtime): handle null content in anthropic message builder#11756

Merged
arvinxx merged 5 commits intonextfrom
fix/anthropic-null-content
Jan 23, 2026
Merged

πŸ› fix(model-runtime): handle null content in anthropic message builder#11756
arvinxx merged 5 commits intonextfrom
fix/anthropic-null-content

Conversation

@arvinxx
Copy link
Copy Markdown
Member

@arvinxx arvinxx commented Jan 23, 2026

Summary

  • Fix TypeError: Cannot read properties of null (reading 'map') when building Anthropic messages
  • Handle assistant messages with tool_calls but null content
  • Handle tool messages with null or empty string content
  • Use <empty_content> placeholder for null/empty content

Problem

When using MCP tools (e.g., Apollo-MCP), some responses may have:

  1. Assistant messages: tool_calls array is non-empty but content is null
  2. Tool messages: content is null or empty string ""

This caused two errors:

  • TypeError: Cannot read properties of null (reading 'map') at buildArrayContent
  • messages.X.content.0.tool_result.content.0.text.text: Input should be a valid string

Solution

Fix 1: Assistant message handling (anthropic.ts:117-121)

const rawContent =
  typeof content === 'string'
    ? ([{ text: message.content, type: 'text' }] as UserMessageContentPart[])
    : Array.isArray(content)
      ? content
      : ([{ text: '<empty_content>', type: 'text' }] as UserMessageContentPart[]);

Fix 2: Tool message handling (anthropic.ts:186)

const toolContent = !message.content ? '<empty_content>' : message.content;

Test plan

  • Added test: should handle assistant message with tool_calls but null content
  • Added test: should handle tool message with null content
  • Added test: should handle tool message with empty string content
  • All 29 tests pass

Related Issues

Closes: LOBE-4201, LOBE-2715

πŸ€– Generated with Claude Code

Fix TypeError when building Anthropic messages with null content:
- Handle assistant messages with tool_calls but null content
- Handle tool messages with null or empty string content
- Use '<empty_content>' placeholder for null/empty content

Add 3 test cases covering the null content scenarios.

Closes: LOBE-4201, LOBE-2715

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown

vercel bot commented Jan 23, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
lobehub Canceled Canceled Jan 24, 2026 1:28am

Request Review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Sorry @arvinxx, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 23, 2026
@gru-agent
Copy link
Copy Markdown
Contributor

gru-agent bot commented Jan 23, 2026

TestGru Assignment

Summary

Link CommitId Status Reason
Detail 62bc803 βœ… Finished

History Assignment

Files

File Pull Request
packages/model-runtime/src/core/contextBuilders/anthropic.ts ❌ Failed (I failed to setup the environment.)

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

@dosubot dosubot bot added the πŸ› Bug label Jan 23, 2026
arvinxx and others added 3 commits January 23, 2026 23:44
Tool messages may have array content, not just string. Use
buildArrayContent to properly process array content in tool results.

Add test case for tool message with array content.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ages

When assistant message has tool_calls but null/empty content, filter
out the empty text block instead of using placeholder. Only tool_use
blocks remain in the content array.

Add test case for empty string content scenario.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add test case to verify tool message with array content containing
both text and image is correctly processed.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@arvinxx arvinxx force-pushed the fix/anthropic-null-content branch from 504a5b5 to 36a13d7 Compare January 23, 2026 15:55
…pty content

Add test cases for tool messages without corresponding assistant
tool_call when content is null or empty string.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 23, 2026

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
βœ… Project coverage is 74.16%. Comparing base (5d115ef) to head (da01632).
⚠️ Report is 13 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next   #11756      +/-   ##
==========================================
- Coverage   74.16%   74.16%   -0.01%     
==========================================
  Files        1193     1193              
  Lines       95004    95014      +10     
  Branches    12534    10499    -2035     
==========================================
+ Hits        70464    70469       +5     
- Misses      24450    24455       +5     
  Partials       90       90              
Flag Coverage Ξ”
app 67.22% <ΓΈ> (ΓΈ)
database 91.48% <ΓΈ> (ΓΈ)
packages/agent-runtime 90.20% <ΓΈ> (ΓΈ)
packages/context-engine 85.33% <ΓΈ> (ΓΈ)
packages/conversation-flow 92.28% <ΓΈ> (ΓΈ)
packages/file-loaders 87.04% <ΓΈ> (ΓΈ)
packages/memory-user-memory 69.30% <ΓΈ> (ΓΈ)
packages/model-bank 100.00% <ΓΈ> (ΓΈ)
packages/model-runtime 86.69% <86.66%> (-0.03%) ⬇️
packages/prompts 79.33% <ΓΈ> (ΓΈ)
packages/python-interpreter 92.90% <ΓΈ> (ΓΈ)
packages/ssrf-safe-fetch 0.00% <ΓΈ> (ΓΈ)
packages/utils 93.22% <ΓΈ> (ΓΈ)
packages/web-crawler 95.62% <ΓΈ> (ΓΈ)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Ξ”
Store 67.98% <ΓΈ> (ΓΈ)
Services 50.73% <ΓΈ> (ΓΈ)
Server 67.71% <ΓΈ> (ΓΈ)
Libs 40.36% <ΓΈ> (ΓΈ)
Utils 93.60% <ΓΈ> (ΓΈ)
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@arvinxx arvinxx merged commit 539753a into next Jan 23, 2026
40 of 43 checks passed
@arvinxx arvinxx deleted the fix/anthropic-null-content branch January 23, 2026 17:57
@lobehubbot
Copy link
Copy Markdown
Member

❀️ Great PR @arvinxx ❀️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.

lobehubbot pushed a commit that referenced this pull request Jan 23, 2026
## [Version&nbsp;2.0.0-next.357](v2.0.0-next.356...v2.0.0-next.357)
<sup>Released on **2026-01-23**</sup>

#### πŸ› Bug Fixes

- **model-runtime**: Handle null content in anthropic message builder.
- **misc**: Page content switch mismatch.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's fixed

* **model-runtime**: Handle null content in anthropic message builder, closes [#11756](#11756) ([539753a](539753a))
* **misc**: Page content switch mismatch, closes [#11758](#11758) ([fdc8f95](fdc8f95))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
JamieStivala pushed a commit to jaworldwideorg/OneJA-Bot that referenced this pull request Jan 23, 2026
## [Version&nbsp;1.154.0](v1.153.1...v1.154.0)
<sup>Released on **2026-01-23**</sup>

#### β™» Code Refactoring

- **misc**: Migrate AI Rules to Claude Code Skills.

#### ✨ Features

- **database**: Extended async task with metadata and parent id, added index.
- **misc**: Remove NextAuth.

#### πŸ› Bug Fixes

- **copilot**: History popover not refreshing when agentId changes.
- **editor**: Prevent crash when toggling enableInputMarkdown setting.
- **home**: Use correct CreateGroupModal for session group creation.
- **model-runtime**: Handle null content in anthropic message builder.
- **ModelSelect**: Resolve tooltip hover causing popup to close.
- **pdf**: Ensure worker config before Document render.
- **store**: Delete message before regeneration.
- **misc**: Fix auto scroll, fix favorite refresh bug and group topic refresh issue, fixed the agent group builder tools excaution edge case crash, page content switch mismatch, when use market group, the group sys role was not used.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

* **misc**: Migrate AI Rules to Claude Code Skills, closes [lobehub#11737](https://github.com/jaworldwideorg/OneJA-Bot/issues/11737) ([346fc46](346fc46))

#### What's improved

* **database**: Extended async task with metadata and parent id, added index, closes [lobehub#11712](https://github.com/jaworldwideorg/OneJA-Bot/issues/11712) ([31d2f26](31d2f26))
* **misc**: Remove NextAuth, closes [lobehub#11732](https://github.com/jaworldwideorg/OneJA-Bot/issues/11732) ([1eff864](1eff864))

#### What's fixed

* **copilot**: History popover not refreshing when agentId changes, closes [lobehub#11731](https://github.com/jaworldwideorg/OneJA-Bot/issues/11731) ([64f39e7](64f39e7))
* **editor**: Prevent crash when toggling enableInputMarkdown setting, closes [lobehub#11755](https://github.com/jaworldwideorg/OneJA-Bot/issues/11755) ([ea5eed8](ea5eed8))
* **home**: Use correct CreateGroupModal for session group creation, closes [lobehub#11752](https://github.com/jaworldwideorg/OneJA-Bot/issues/11752) ([36bcc50](36bcc50))
* **model-runtime**: Handle null content in anthropic message builder, closes [lobehub#11756](https://github.com/jaworldwideorg/OneJA-Bot/issues/11756) ([539753a](539753a))
* **ModelSelect**: Resolve tooltip hover causing popup to close, closes [lobehub#11742](https://github.com/jaworldwideorg/OneJA-Bot/issues/11742) ([1b73f14](1b73f14))
* **pdf**: Ensure worker config before Document render, closes [lobehub#11746](https://github.com/jaworldwideorg/OneJA-Bot/issues/11746) ([ad34072](ad34072))
* **store**: Delete message before regeneration, closes [lobehub#11760](https://github.com/jaworldwideorg/OneJA-Bot/issues/11760) ([a8a6300](a8a6300))
* **misc**: Fix auto scroll, closes [lobehub#11734](https://github.com/jaworldwideorg/OneJA-Bot/issues/11734) ([892fa9f](892fa9f))
* **misc**: Fix favorite refresh bug and group topic refresh issue, closes [lobehub#11745](https://github.com/jaworldwideorg/OneJA-Bot/issues/11745) ([5d115ef](5d115ef))
* **misc**: Fixed the agent group builder tools excaution edge case crash, closes [lobehub#11735](https://github.com/jaworldwideorg/OneJA-Bot/issues/11735) ([5de4742](5de4742))
* **misc**: Page content switch mismatch, closes [lobehub#11758](https://github.com/jaworldwideorg/OneJA-Bot/issues/11758) ([fdc8f95](fdc8f95))
* **misc**: When use market group, the group sys role was not used, closes [lobehub#11739](https://github.com/jaworldwideorg/OneJA-Bot/issues/11739) ([afc76f9](afc76f9))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

πŸ› Bug size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants