opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: cloudflare-ai-gateway sdk.chat undefined error

Open englishm opened this issue 3 months ago • 5 comments

Summary

Fixes the sdk.chat is not a function error when using Cloudflare AI Gateway as a provider.

  • Changed sdk.chat(modelID) to sdk.languageModel(modelID) in the cloudflare-ai-gateway custom loader

Problem

The @ai-sdk/openai-compatible SDK (which Cloudflare AI Gateway uses per models.dev) does not have a .chat() method. It only exposes .languageModel() and .chatModel(). This caused the error:

ERROR sdk.chat is not a function. (In 'sdk.chat(modelID)', 'sdk.chat' is undefined)

The original PR #5174 was merged with sdk.chat(), even though the author noted in a comment that they had "fixed the way we call the openai-compatible SDK (.languageModel)" - that fix was never actually committed.

Solution

Use .languageModel() which is the standard method available on all AI SDK providers and matches how other providers in this file work (see bedrock, google-vertex, sap-ai-core, etc.).

Testing

Tested locally with Cloudflare AI Gateway credentials - models now load and respond correctly.


This fix was developed entirely using opencode itself 🎉

Fixes #6262

englishm avatar Dec 29 '25 23:12 englishm

Written by Anthropic Claude Opus 4.5 in opencode itself: https://opncd.ai/share/jQrbNtGy

englishm avatar Dec 29 '25 23:12 englishm

cc: @elithrar

englishm avatar Dec 29 '25 23:12 englishm

Which models did you get with this + which version of OpenCode?

(Tested this prior and it worked, so wondering why this/how this got missed!)

elithrar avatar Dec 29 '25 23:12 elithrar

@elithrar:

Which models did you get with this + which version of OpenCode?

(Tested this prior and it worked, so wondering why this/how this got missed!)

This was broken for me on 1.0.203, 1.0.204, and 1.0.207. Tested with Opus 4.5 and Sonnet 4.5 (broken before this change, working after).

My suspicion was that a dependency might have shifted and broken this, but Claude thinks you forgot to push a commit you had locally to make this same change before your PR was merged.

englishm avatar Dec 29 '25 23:12 englishm

@rekram1-node could I get a favor + have this merged? 🙏

elithrar avatar Jan 02 '26 11:01 elithrar

@elithrar yes ofc, I read through this and thought it wasnt necessary given your comments but lgtm so sure!

rekram1-node avatar Jan 02 '26 17:01 rekram1-node