Skip to content

Handle bundled ReadableStream responses in ModelResult#33

Closed
w0nche0l wants to merge 1 commit intoOpenRouterTeam:mainfrom
w0nche0l:codex/readable-stream-detection
Closed

Handle bundled ReadableStream responses in ModelResult#33
w0nche0l wants to merge 1 commit intoOpenRouterTeam:mainfrom
w0nche0l:codex/readable-stream-detection

Conversation

@w0nche0l
Copy link
Copy Markdown

@w0nche0l w0nche0l commented May 1, 2026

Summary

  • the agent package was failing when deployed to a production OpenNext Cloudflare Worker deployment (even though it worked fine in local workerd workflow)
  • Removes the brittle constructor.name === "EventStream" check.
  • Adds a direct value instanceof ReadableStream inheritance check.

Verified Repro

I verified the naming check with a fresh minimal OpenNext Cloudflare Worker project using:

  • next@16.2.4
  • @opennextjs/cloudflare@1.19.5
  • wrangler@4.87.0
  • @openrouter/agent@0.4.0

Minimal route:

import { OpenRouter, stepCountIs } from "@openrouter/agent";

export const runtime = "nodejs";

export async function GET() {
  const openrouter = new OpenRouter({
    apiKey: process.env.OPENROUTER_API_KEY,
  });

  const result = openrouter.callModel({
    model: process.env.OPENROUTER_MODEL || "google/gemini-3-flash-preview",
    input: "Reply with exactly: ok",
    instructions: "Return a very short answer.",
    stopWhen: stepCountIs(1),
  });

  const [text, response] = await Promise.all([
    result.getText(),
    result.getResponse(),
  ]);

  return Response.json({
    text,
    object: response.object,
  });
}

Deploying that project to Cloudflare Workers with real OPENROUTER_API_KEY and OPENROUTER_MODEL secrets returned:

HTTP/2 500

wrangler tail showed:

Error: Unexpected response type from API

Expected result:

{
  "text": "ok",
  "object": "response"
}

With this diff as a local patch, it succeeded.

@w0nche0l w0nche0l force-pushed the codex/readable-stream-detection branch 2 times, most recently from 5a58072 to bc9d619 Compare May 1, 2026 07:57
@w0nche0l w0nche0l force-pushed the codex/readable-stream-detection branch 3 times, most recently from a18685b to a324625 Compare May 1, 2026 08:28
@robert-j-y robert-j-y self-requested a review May 1, 2026 19:26
@robert-j-y
Copy link
Copy Markdown
Collaborator

Thanks for the fix. I’d recommend closing this one in favor of #34, since the two PRs are overlapping fixes to the same stream-detection bug rather than independent changes.

This PR adds ReadableStream detection but keeps the old toReadableStream() fallback. #34 checks for getReader(), which better matches the actual runtime contract used downstream by ReusableReadableStream, and it also updates the non-streaming response guard consistently.

If there’s anything from this PR you want to preserve, I’d fold it into #34; otherwise #34 should be the base to move forward with after it adds the patch changeset and regression tests.

@w0nche0l
Copy link
Copy Markdown
Author

w0nche0l commented May 1, 2026

sounds good! primarily submitted both because i couldn't tell if the getReader check was intentional. We probably do want to remove the name check either way in #34 though. closing this out!

@w0nche0l w0nche0l closed this May 1, 2026
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.

2 participants