Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

📭 fix: Drop Empty Summary Parts Before Persistence (#15338)

Danny Avilae003eeac56c9
diff --git a/packages/api/src/utils/content.spec.ts b/packages/api/src/utils/content.spec.ts
index d5632f75f8..693fce4fc0 100644
--- a/packages/api/src/utils/content.spec.ts
+++ b/packages/api/src/utils/content.spec.ts
@@ -45,6 +45,41 @@ describe('filterMalformedContentParts', () => {
expect(result).toEqual(parts);
});
+ it('filters empty summary parts before persistence', () => {
+ const parts = [
+ { type: ContentTypes.SUMMARY },
+ { type: ContentTypes.SUMMARY, content: [] },
+ { type: ContentTypes.SUMMARY, content: [{ type: ContentTypes.TEXT, text: ' ' }] },
+ { type: ContentTypes.SUMMARY, text: '\n\t' },
+ { type: ContentTypes.TEXT, text: 'Final answer' },
+ ] as TMessageContentParts[];
+
+ expect(filterMalformedContentParts(parts)).toEqual([
+ { type: ContentTypes.TEXT, text: 'Final answer' },
+ ]);
+ });
+
+ it('keeps current and legacy summary parts that contain text', () => {
+ const current = {
+ type: ContentTypes.SUMMARY,
+ content: [{ type: ContentTypes.TEXT, text: 'Current summary' }],
+ };
+ const legacyContent = {
+ type: ContentTypes.SUMMARY,
+ content: 'Legacy content summary',
+ };
+ const legacyText = {
+ type: ContentTypes.SUMMARY,
+ text: 'Legacy text summary',
+ };
+
+ expect(filterMalformedContentParts([current, legacyContent, legacyText])).toEqual([
+ current,
+ legacyContent,
+ legacyText,
+ ]);
+ });
+
it('should filter out null or undefined parts', () => {
const parts = [
{ type: ContentTypes.TEXT, text: 'Valid' },
diff --git a/packages/api/src/utils/content.ts b/packages/api/src/utils/content.ts
index 8a39d2a867..31956cb3ed 100644
--- a/packages/api/src/utils/content.ts
+++ b/packages/api/src/utils/content.ts
@@ -2,6 +2,27 @@ import { ContentTypes } from 'librechat-data-provider';
import type { TMessageContentParts } from 'librechat-data-provider';
type ActivityLabelPart = Extract<TMessageContentParts, { type: ContentTypes.ACTIVITY_LABEL }>;
+type SummaryPart = Extract<TMessageContentParts, { type: ContentTypes.SUMMARY }>;
+type RuntimeSummaryPart = Omit<SummaryPart, 'content'> & {
+ content?: SummaryPart['content'] | string;
+ text?: string;
+};
+
+function hasSummaryText(part: RuntimeSummaryPart): boolean {
+ if (typeof part.content === 'string') {
+ return part.content.trim().length > 0;
+ }
+
+ if (Array.isArray(part.content)) {
+ for (const block of part.content) {
+ if (typeof block?.text === 'string' && block.text.trim().length > 0) {
+ return true;
+ }
+ }
+ }
+
+ return typeof part.text === 'string' && part.text.trim().length > 0;
+}
function isRetainablePart(
part: TMessageContentParts | null | undefined,
@@ -14,6 +35,10 @@ function isRetainablePart(
return 'tool_call' in part && part.tool_call != null && typeof part.tool_call === 'object';
}
+ if (part.type === ContentTypes.SUMMARY) {
+ return hasSummaryText(part);
+ }
+
return true;
}
@@ -35,8 +60,8 @@ function rebaseBound(bound: number, retainedBefore: number[], sourceLength: numb
}
/**
- * Removes malformed tool call parts and any empty slots, then rebases parent
- * activity-phase bounds onto the compacted coordinates.
+ * Removes malformed tool calls, empty summaries, and empty slots, then rebases
+ * parent activity-phase bounds onto the compacted coordinates.
*
* The source array is frequently sparse: the aggregator writes parts at
* provider-source indexes, so a model turn that emits no text before its tool
@@ -95,9 +120,9 @@ function compactContentParts(contentParts: TMessageContentParts[]): TMessageCont
}
/**
- * Produces the durable content array: drops malformed tool call parts that lack the
- * required tool_call property, compacts away empty slots, and rebases parent
- * activity-phase bounds onto the resulting coordinates.
+ * Produces the durable content array: drops malformed tool calls and empty summaries,
+ * compacts away empty slots, and rebases parent activity-phase bounds onto the
+ * resulting coordinates.
*
* Compaction is not incidental — the source array is frequently sparse, because the
* aggregator writes parts at provider-source indexes. Since that shifts positions, any