Skip to content

Keep role bytes when the template role cache is full - #1483

Merged
martindevans merged 1 commit into
SciSharp:masterfrom
ewerton336:fix/template-role-cache-full
Nov 6, 2026
Merged

martindevans merged 1 commit into
SciSharp:masterfrom
ewerton336:fix/template-role-cache-full

Conversation

@ewerton336

Copy link
Copy Markdown
Contributor

LLamaTemplate.TextMessage caches the UTF-8 bytes of each role, with a hard limit of 128 entries. The assignment roleBytes = rArr; was inside the if (roleCache.Count < 128) block, so once the cache was full any new role ended up with empty RoleBytes. When the template is applied, that empty memory is pinned to a null pointer and passed to llama_chat_apply_template as the role.

This moves the assignment out of the size check, so the converted bytes are always used and only the caching is limited.

Added a test that adds 200 distinct roles to a template and checks every message keeps its role bytes. It doesn't need a model, so it runs without the downloaded test models.

@martindevans martindevans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oops, thanks for fixing this!

@martindevans
martindevans merged commit bf8ad92 into SciSharp:master Nov 6, 2026
8 checks passed
@ewerton336
ewerton336 deleted the fix/template-role-cache-full branch November 6, 2026 15:23
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