-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: use ai-sdk openai chat language model instead of completion language model #3204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use ai-sdk openai chat language model instead of completion language model #3204
Conversation
the fix is super small but the reason is kind of interesting. IIUC, the intention of sst#2528 was to allow Azure users to hit a chat completions endpoint (`%baseURL%/deployments/%Model%/chat/completions?api-version=%apiVersion%`) instead of a responses endpoint. i work somewhere that currently only has access to a chat completions endpoint, but when i tried this out, the appropriate configuration still errored. i was connecting to the expected endpoint, but AI SDK was complaining that the prompt was invalid. turns out, both `sdk.completion` and `sdk.chat` ultimately hit a `/chat/completions` endpoint when called here by opencode. however, i'm pretty sure `sdk.completion` is *supposed to* hit `/completions` instead of `/chat/completions`; i haven't tracked down why, but that's what happens AFAICT. anyway, the code paths for a chat language model differ a bit from a completion language model. most notably, a completion language model [complains if the prompt has more than one system message](https://github.com/vercel/ai/blob/34407b23b8c276359ac8dbeb42cc06f9db301b99/packages/openai/src/completion/convert-to-openai-completion-prompt.ts#L31) while a chat language model does not care. since opencode [may include more than one system message](https://github.com/sst/opencode/blob/1c59530115d05e0f18f381023f639accc851825c/packages/opencode/src/session/prompt.ts#L413), the completions model would complain. swapping to a chat language model solves this problem. also, i think it's just more accurate with respect to what functionality is supposed to be exposed.
This is very detailed, and I like it, can you clarify one thing (i haven't looked into the azure provider enough): What is a little weird to me is all the other people had /chat/completions and the guy that made pr said he had it working? Any idea why it was working for him but not you? the paths you are using seem the same |
honestly i dont know, mostly because i dont know why given that the error is specifically thrown because of multiple system messages, i would have guessed that they happened to not have multiple system messages in their prompt. OTOH, it looks like |
@enlilz can you test this by chance with your setup? What's weird here is that the vercel ai sdk docs seem to indicate that .completions should work: https://ai-sdk.dev/providers/ai-sdk-providers/azure#chat-models Oo from vercel ai source code: /**
Creates an Azure OpenAI chat model for text generation.
*/
chat(deploymentId: string): LanguageModelV3;
/**
Creates an Azure OpenAI responses API model for text generation.
*/
responses(deploymentId: string): LanguageModelV3;
/**
Creates an Azure OpenAI completion model for text generation.
*/
completion(deploymentId: string): LanguageModelV3; |
Ahhh I think it's because of the api version 2025-01-01-preview @seridescent what happens if you add this to your opencode.json config:
^ I think this should make it send that as the apiVersion @seridescent if you can confirm that doing this^^ works on pushed code, do you think we could adjust the conditional to check the apiVersion? If it is I think that should work as expected |
i have run v0.15.3 both with and without im also curious, where is this branch to |
I was looking at this commit: vercel/ai@3771a27 And I was trying to identify any difference between those other peoples setup and your own |
got it. yeah the apparent inconsistency really is bizarre. idt also FWIW, the chat models snippet you linked on the vercel ai-sdk docs uses plain |
I saw a comment somewhere about the compatibility the azure api sends data back, so I was wondering if that is what was causing the issue, not necessarily that the logic was handled at the ai sdk level Hm okay, this makes sense to me, I think we can go ahead and merge and then address any hiccups later |
@rekram1-node tested it with 0.15.4 (that should include this change) works fine for me, no issues found on a quick test. i still see some issues with url generation for Azure Ai models, gpt-5-mini breaks all the possible configuration for me (would also not work with the old options before this pr) gpt-5-chat provides a url in azure that can be used by the configuration. @seridescent what models are you currently using in Azure Ai ? maybe you can also have a look at opencode doc pr and give your input |
thanks! |
the fix is super small but the reason is kind of interesting.
IIUC, the intention of #2528 was to allow Azure users to hit a chat completions endpoint
(
%baseURL%/deployments/%Model%/chat/completions?api-version=%apiVersion%
) instead of a responses endpoint.i work somewhere that currently only has access to a chat completions endpoint, but when i tried this out, the appropriate configuration still errored. i was connecting to the expected endpoint, but AI SDK was complaining that the prompt was invalid.
turns out, both
sdk.completion
andsdk.chat
ultimately hit a/chat/completions
endpoint when called here by opencode. however, i'm pretty suresdk.completion
is supposed to hit/completions
instead of/chat/completions
; i haven't tracked down why, but that's what happens AFAICT.anyway, the code paths for a chat language model differ a bit from a completion language model. most notably, a completion language model complains if the prompt has more than one system message while a chat language model does not care.
since opencode may include more than one system message, the completions model would complain.
swapping to a chat language model solves this problem. also, i think it's just more accurate with respect to what functionality is supposed to be exposed.
related: #2528 , #1508 , #3150