Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/agents/processors/content_processor_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ function rearrangeEventsForLatestFunctionResponse(events: Event[]): Event[] {
}

if (!match) {
throw new Error(
`No function call event found for function responses ids: ${Array.from(
functionResponsesIds,
).join(', ')}`,
);
// If the function call event cannot be found (e.g. due to context compaction
// rewriting the history before we parse it), we cannot safely include the
// function response event since the LLM inherently requires the call before
// the response. Drop the response event from the current request payload.
return events.slice(0, events.length - 1);
}

// Collect all function response events between the function call event
Expand Down
9 changes: 5 additions & 4 deletions core/test/agents/processors/content_processor_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,11 @@ describe('getContents', () => {
],
},
});

expect(() => getContents([e0, e1, e2], 'my_agent')).toThrowError(
'No function call event found for function responses ids: id2',
);
const result = getContents([e0, e1, e2], 'my_agent');
expect(result).toHaveLength(2);
// Should drop the last orphaned response event
expect(result[0].role).toBe('user');
expect(result[1].role).toBe('model');
});

it('should throw subset error when response is for an id from a call event, but contains other unexpected ids', () => {
Expand Down
Loading