🫙 fix: Drop Blank Content Blocks From Promptless Sends (#15530)

* 🫙 fix: Drop Blank Content Blocks From Promptless Sends

* fix: Preserve the promptless turn instead of dropping it
This commit is contained in:
Danny Avila 2026-09-02 20:15:00 -04:00 • committed by GitHub
parent 790f2b3153
commit 676cd512b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 2 deletions

View file

@ -622,4 +622,59 @@ describe('formatAgentMessages', () => {
expect(result[0].content).toEqual(media);
});
});
describe('promptless sends', () => {
const PLACEHOLDER = [{ type: 'text', text: '(no text)' }];
it('should stand in for a replayed promptless turn instead of a blank block', () => {
const result = formatAgentMessages([
{ role: 'user', content: '' },
{ role: 'assistant', content: 'Hi' },
]);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(HumanMessage);
expect(result[0].content).toEqual(PLACEHOLDER);
expect(result[0].content).not.toContainEqual(
expect.objectContaining({ type: 'text', text: '' }),
);
});
it('should keep the turn so assistant messages never become adjacent', () => {
const result = formatAgentMessages([
{ role: 'assistant', content: 'first' },
{ role: 'user', content: '' },
{ role: 'assistant', content: 'second' },
]);
expect(result).toHaveLength(3);
expect(result[1]).toBeInstanceOf(HumanMessage);
});
it('should not emit a blank text block for a whitespace-only user message', () => {
const result = formatAgentMessages([{ role: 'user', content: ' ' }]);
expect(result).toHaveLength(1);
expect(result[0].content).toEqual(PLACEHOLDER);
});
it('should keep a promptless user message that still carries its images', () => {
const image_urls = [{ type: 'image_url', image_url: { url: 'data:image/png;base64,AAA' } }];
const result = formatAgentMessages([{ role: 'user', content: '', image_urls }]);
expect(result).toHaveLength(1);
expect(result[0].content).toEqual(image_urls);
expect(result[0].content).not.toContainEqual(
expect.objectContaining({ type: 'text', text: '' }),
);
});
it('should still format a user message that has text', () => {
const result = formatAgentMessages([{ role: 'user', content: 'hello' }]);
expect(result).toHaveLength(1);
expect(result[0]).toBeInstanceOf(HumanMessage);
expect(result[0].content).toEqual([{ type: 'text', text: 'hello' }]);
});
});
});

View file

@ -7,6 +7,12 @@ const {
SystemMessage,
} = require('@librechat/agents/langchain/messages');
/**
* Stands in for a user turn that carries no text and whose attachments are no longer
* being resent, so the turn stays valid without inventing content it never had.
*/
const EMPTY_MESSAGE_PLACEHOLDER = '(no text)';
/**
* Formats a message to OpenAI Vision API payload format.
*
@ -163,10 +169,32 @@ const formatAgentMessages = (payload) => {
for (const message of payload) {
if (typeof message.content === 'string') {
message.content = [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: message.content }];
/** An empty string yields a blank text block, which strict providers (Bedrock,
* Anthropic) reject outright for the whole request. `formatVisionMessage`
* already guards this for image-bearing sends; history replay of a
* promptless send reaches here with no `image_urls`, so guard it too. */
message.content = message.content.trim()
? [{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: message.content }]
: [];
}
if (message.role !== 'assistant') {
messages.push(formatMessage({ message, langChain: true }));
const formatted = formatMessage({ message, langChain: true });
/** A promptless send replayed from history can reduce to nothing once its
* attachments are no longer resent. Providers reject a blank text block and
* an empty content array alike, but dropping the turn is not safe either:
* nothing merges the assistant turns it would leave adjacent, and the same
* providers reject consecutive assistant messages. Keep the turn, and give
* it the smallest honest stand-in for the content that is no longer there. */
const { content: formattedContent } = formatted;
const isEmpty = Array.isArray(formattedContent)
? formattedContent.length === 0
: typeof formattedContent === 'string' && formattedContent.trim() === '';
if (isEmpty) {
formatted.content = [
{ type: ContentTypes.TEXT, [ContentTypes.TEXT]: EMPTY_MESSAGE_PLACEHOLDER },
];
}
messages.push(formatted);
continue;
}