Skip to content

fix(reka-ai): guard first-message access in chat transform#1712

Open
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/reka-ai-empty-messages
Open

fix(reka-ai): guard first-message access in chat transform#1712
ankit25bcs10610 wants to merge 1 commit into
Portkey-AI:mainfrom
ankit25bcs10610:fix/reka-ai-empty-messages

Conversation

@ankit25bcs10610

Copy link
Copy Markdown

What

The conversation_history transform in RekaAIChatCompleteConfig dereferences messages[0] (a local accumulator array) before anything is pushed to it:

const messages: RekaMessageItem[] = [];
...
if (media_url && messages[0].media_url) { ... }  // messages still empty
...
if (messages[0].type !== 'human') { ... }        // empty / fully-filtered list
  • The first check throws TypeError: Cannot read properties of undefined when the first message is an image (media_url truthy, messages still empty).
  • The second throws when the resulting message list is empty (e.g. no messages, or all filtered out).

Fix

Optional-chain both accesses (messages[0]?.media_url, messages[0]?.type), so an image-first or empty message list is handled gracefully.

Tests

Added src/providers/reka-ai/chatComplete.test.ts covering both cases. Verified each fails before the fix (throws) and passes after.

npm run format:check passes.

The conversation_history transform dereferenced `messages[0]` before any
element was pushed to the local array:

- `if (media_url && messages[0].media_url)` throws when the first message
  is an image (messages is still empty at that point).
- `if (messages[0].type !== 'human')` throws when the message list is
  empty (or fully filtered).

Use optional chaining so an image-first or empty message list no longer
crashes the transform. Add regression tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant