Skip to content

StreamingTextResponse not compatible with Remix action function #199

@cephalization

Description

@cephalization

When using the package in an action, like so,

export const action = async ({ request }: ActionArgs) => {
    const { messages, transcriptId } = z
        .object({
            messages: z.array(
                z
                    .object({
                        content: z.string(),
                        role: z.union([z.literal('user'), z.literal('system'), z.literal('assistant')]),
                    })
                    .passthrough(),
            ),
            transcriptId: z.string(),
        })
        .parse(await request.json());

    // Load transcript set from Redis, turn it into a string
    const transcriptArray = await transcriptQueries.getTranscriptArray(redisClient, { transcriptKey: transcriptId });
    const transcript = transcriptArray;
    const prompt = `<my prompt>`;
    const content = prompt.replace('{transcript}', constrainLinesToTokenLimit(transcript, prompt, 6000).join('\n'));

    const newMessages = [
        {
            role: 'system',
            content,
        },
        ...messages,
    ] as const;

    const response = await openai.createChatCompletion({
        // model: 'gpt-3.5-turbo-16k',
        model: 'gpt-4',
        // @ts-expect-error
        messages: newMessages,
        stream: true,
    });
    const stream = OpenAIStream(response);

    return new StreamingTextResponse(stream);
};

The action returns an internal error

TypeError: First parameter has member 'readable' that is not a ReadableStream.
dashboard:dev:     at assertReadableStream (/Users/tony/repos/teno/node_modules/web-streams-polyfill/src/lib/validators/readable-stream.ts:5:11)
dashboard:dev:     at convertReadableWritablePair (/Users/tony/repos/teno/node_modules/web-streams-polyfill/src/lib/validators/readable-writable-pair.ts:15:3)
dashboard:dev:     at ReadableStream.pipeThrough (/Users/tony/repos/teno/node_modules/web-streams-polyfill/src/lib/readable-stream.ts:211:23)
dashboard:dev:     at AIStream (/Users/tony/repos/teno/node_modules/ai/dist/index.js:147:29)
dashboard:dev:     at OpenAIStream (/Users/tony/repos/teno/node_modules/ai/dist/index.js:170:10)
dashboard:dev:     at action7 (/Users/tony/repos/teno/apps/dashboard/app/routes/api.chat.ts:68:17)
dashboard:dev:     at processTicksAndRejections (node:internal/process/task_queues:95:5)
dashboard:dev:     at Object.callRouteActionRR (/Users/tony/repos/teno/node_modules/@remix-run/node/node_modules/@remix-run/server-runtime/dist/data.js:35:16)
dashboard:dev:     at callLoaderOrAction (/Users/tony/repos/teno/node_modules/@remix-run/router/router.ts:3568:16)
dashboard:dev:     at submit (/Users/tony/repos/teno/node_modules/@remix-run/router/router.ts:2835:16)
dashboard:dev:     at queryImpl (/Users/tony/repos/teno/node_modules/@remix-run/router/router.ts:2770:22)
dashboard:dev:     at Object.queryRoute (/Users/tony/repos/teno/node_modules/@remix-run/router/router.ts:2720:18)
dashboard:dev:     at handleResourceRequestRR (/Users/tony/repos/teno/node_modules/@remix-run/node/node_modules/@remix-run/server-runtime/dist/server.js:251:20)
dashboard:dev:     at requestHandler (/Users/tony/repos/teno/node_modules/@remix-run/node/node_modules/@remix-run/server-runtime/dist/server.js:59:18)
dashboard:dev:     at /Users/tony/repos/teno/node_modules/@remix-run/express/dist/server.js:39:22

This appears to be due to some web streams workaround that remix installs. It even occurs when hosted on vercel. This error does not occur if I move all of this code into a dedicated express api but that defeats the purpose of me using remix as it would be my only external api call.

Anyone had luck fixing this? I've tried implementing the solutions here MattiasBuelens/web-streams-polyfill#93 with no luck.

I will try to produce a reproduction repo soon, but using any remix template should immediately reproduce the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions