Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions app/api/transliterate/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { openai, createDirectOpenAI } from '@/lib/openai';
import { openai, createDirectOpenAI, createAzureOpenAI } from '@/lib/openai';
import { buildPrompt, getAllStyles } from '@/lib/styles';
import { TransliterationStyle } from '@/types/transliteration';

Expand All @@ -25,11 +25,40 @@ export async function POST(req: NextRequest) {
const userPrompt = reverse ? `Romanized: ${text}` : `${text}`;

let transliteration = "";

// Check if we should use direct OpenAI for all requests
const useDirectOpenAI = !openai && process.env.OPENAI_API_KEY;

if (useDirectOpenAI) {
// Use direct OpenAI for all styles when Azure is not configured
const directClient = createDirectOpenAI();
const model = style === TransliterationStyle.SHARIASOURCE && !reverse ? "gpt-4.1" : "gpt-4";

const completion = await directClient.chat.completions.create({
model,
temperature: 0,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt }
],
...(style === TransliterationStyle.SHARIASOURCE && !reverse ? {
response_format: { "type": "text" },
max_completion_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
} : {})
});
Comment on lines +37 to +51
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix: max_completion_tokens is not a valid Chat Completions parameter; use max_tokens

For the Chat Completions API, the correct parameter is max_tokens. Using max_completion_tokens may be ignored or cause errors depending on SDK/runtime.

       const completion = await directClient.chat.completions.create({
         model,
         temperature: 0,
         messages: [
           { role: "system", content: systemPrompt },
           { role: "user", content: userPrompt }
         ],
-        ...(style === TransliterationStyle.SHARIASOURCE && !reverse ? {
-          response_format: { "type": "text" },
-          max_completion_tokens: 2048,
+        ...(style === TransliterationStyle.SHARIASOURCE && !reverse ? {
+          // response_format is optional for Chat Completions; default is text.
+          max_tokens: 2048,
           top_p: 1,
           frequency_penalty: 0,
           presence_penalty: 0
         } : {})
       });

Optional: Since default output is text for Chat Completions, you can remove response_format entirely here.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const completion = await directClient.chat.completions.create({
model,
temperature: 0,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt }
],
...(style === TransliterationStyle.SHARIASOURCE && !reverse ? {
response_format: { "type": "text" },
max_completion_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
} : {})
});
const completion = await directClient.chat.completions.create({
model,
temperature: 0,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt }
],
...(style === TransliterationStyle.SHARIASOURCE && !reverse ? {
// response_format is optional for Chat Completions; default is text.
max_tokens: 2048,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0
} : {})
});
🤖 Prompt for AI Agents
In app/api/transliterate/route.ts between lines 37 and 51, replace the invalid
parameter max_completion_tokens with max_tokens in the chat completions create
call. Additionally, you can remove the response_format property entirely since
the default output for Chat Completions is text. This will ensure the API call
uses the correct parameters and avoids potential errors.


if (style === TransliterationStyle.SHARIASOURCE && !reverse) {
const result = completion.choices[0]?.message.content?.trim() ?? "";
transliteration = style === TransliterationStyle.SHARIASOURCE && !reverse
? result.replace(/\*/g, '')
: result;
} else if (style === TransliterationStyle.SHARIASOURCE && !reverse) {
// Try Azure OpenAI first, fallback to direct OpenAI if it fails
try {
const completion = await openai.chat.completions.create({
const azureClient = openai || createAzureOpenAI();
const completion = await azureClient.chat.completions.create({
Comment on lines +60 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix: max_completion_tokens should be max_tokens for Azure Chat Completions as well

Same parameter issue applies to the Azure call. Suggest removing response_format or keep only if required by your Azure deployment (it’s typically unnecessary for plain text).

-        const azureClient = openai || createAzureOpenAI();
+        const azureClient = openai || createAzureOpenAI();
         const completion = await azureClient.chat.completions.create({
           model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1",
           temperature: 0,
           messages: [
             { role: "system", content: systemPrompt },
             { role: "user", content: userPrompt }
           ],
-          response_format: {
-            "type": "text"
-          },
-          max_completion_tokens: 2048,
+          // Default is text for Chat Completions; response_format can be omitted.
+          max_tokens: 2048,
           top_p: 1,
           frequency_penalty: 0,
           presence_penalty: 0
         });

If you intend to leverage the newer Responses API for GPT‑4.1, we can refactor this call path to client.responses.create instead. Happy to draft that if desired.

Also applies to: 68-75

🤖 Prompt for AI Agents
In app/api/transliterate/route.ts around lines 60-61 and 68-75, the Azure Chat
Completions call incorrectly uses the parameter max_completion_tokens instead of
max_tokens. Update the parameter name to max_tokens for Azure compatibility.
Also, remove the response_format parameter unless your Azure deployment
specifically requires it, as it is generally unnecessary for plain text
responses. If you want to use the newer Responses API for GPT-4.1, consider
refactoring the call to client.responses.create, but this is optional.

model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1",
temperature: 0,
messages: [
Expand Down Expand Up @@ -81,7 +110,8 @@ export async function POST(req: NextRequest) {
}
} else {
// Standard single call for other styles
const completion = await openai.chat.completions.create({
const azureClient = openai || createAzureOpenAI();
const completion = await azureClient.chat.completions.create({
model: process.env.AZURE_4_1_DEPLOYMENT || "snapsolve-gpt4.1",
temperature: 0,
messages: [
Expand Down
28 changes: 17 additions & 11 deletions lib/openai.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { AzureOpenAI, OpenAI } from 'openai';

if (!process.env.AZURE_SECRET_KEY) {
throw new Error('Missing AZURE_SECRET_KEY environment variable');
}
export const createAzureOpenAI = () => {
if (!process.env.AZURE_SECRET_KEY) {
throw new Error('Missing AZURE_SECRET_KEY environment variable');
}

if (!process.env.AZURE_ENDPOINT_URL) {
throw new Error('Missing AZURE_ENDPOINT_URL environment variable');
}
Comment on lines +3 to +10
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Make Azure API version configurable (+ verify validity of '2024-10-21')

Hard-coding the apiVersion can cause breakage if Azure changes requirements. Prefer an env override with a sane default. Also, please double-check that '2024-10-21' is a valid Azure OpenAI API version for your deployments.

Apply this minimal change:

   return new AzureOpenAI({
     apiKey: process.env.AZURE_SECRET_KEY,
     endpoint: process.env.AZURE_ENDPOINT_URL,
-    apiVersion: '2024-10-21',
+    apiVersion: process.env.AZURE_OPENAI_API_VERSION ?? '2024-10-21',
   });

If helpful, I can follow up with a small doc PR to mention AZURE_OPENAI_API_VERSION support in README.

Optionally verify the latest stable API version:

Also applies to: 12-17


🌐 Web query:

What is the latest generally available Azure OpenAI API version that supports GPT‑4.1 chat/completions or the Responses API?

💡 Result:

The latest generally available Azure OpenAI API version that supports GPT‑4.1 chat/completions and the Responses API is 2024-10-21[1][2]. This API version supersedes the previous GA version 2024-06-01 and is currently the stable release for production use in Azure AI Foundry.

Key Details:

  • API Version: 2024-10-21 (GA as of July 2025)[1][2]
  • Supported Models: Includes GPT-4.1 (and its variants like mini and nano)[1][9].
  • Supported APIs: Both Chat Completions and Responses API endpoints are supported in this version[1][2].
  • How to Use for GPT-4.1:
    • For chat/completions, use client.chat.completions.create with api_version="2024-10-21" and your model deployment name (e.g., "gpt-4.1", "gpt-4o")[2].
    • For the Responses API, similar patterns apply using client.responses.create[1].

Code Example for Chat Completions:

import os
from openai import AzureOpenAI

client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    api_version="2024-10-21",  # Latest GA
    azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
)

response = client.chat.completions.create(
    model="gpt-4.1",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the largest mammal?"}
    ]
)
print(response.model_dump_json(indent=2))

[2]

Code Example for Responses API:

import os
from openai import AzureOpenAI

client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    api_version="2024-10-21",  # Latest GA
    azure_endpoint="https://YOUR-RESOURCE-NAME.openai.azure.com"
)

response = client.responses.create(
    model="gpt-4.1-nano",
    input="This is a test."
)
print(response.model_dump_json(indent=2))

[1]

Important Notes:

  • The next generation "v1" API is still in preview as of August 2025. When this API becomes GA, traffic without a specified api-version will route to the latest GA version, but right now only "preview" is supported for v1[1].
  • GPT-4.1 is available for deployment in Azure AI Foundry from April 2025, supporting large context windows and updated capabilities[9].
  • For vision-enabled models, a different (preview) API version might be required (e.g., 2024-02-15-preview)[8].

Summary Table

API Version Status Supports GPT-4.1 Chat/Completions/Responses API
2024-10-21 GA (production) Yes
2025-04-01-preview Preview Yes (newer features, not GA)
Next-gen v1 (preview) Preview Planned future GA, not yet released

This means for stable, production Azure deployments of GPT‑4.1 with chat or Responses API, use API version 2024-10-21[1][2][9].

Citations:


Parameterize Azure OpenAI API Version

Verified that 2024-10-21 is the current GA version supporting GPT-4.1 chat/completions and the Responses API. To guard against future API–version changes, let’s make it configurable via an environment variable:

• Update lib/openai.ts (around lines 12–13):

   return new AzureOpenAI({
     apiKey: process.env.AZURE_SECRET_KEY,
     endpoint: process.env.AZURE_ENDPOINT_URL,
-      apiVersion: '2024-10-21',
+      apiVersion: process.env.AZURE_OPENAI_API_VERSION ?? '2024-10-21',
   });

• Add validation for AZURE_OPENAI_API_VERSION alongside the existing env‐var checks if desired.
• Update README.md to document the new AZURE_OPENAI_API_VERSION option and its default.

🤖 Prompt for AI Agents
In lib/openai.ts around lines 3 to 10, the Azure OpenAI API version is hardcoded
and should be parameterized. Add a check for the environment variable
AZURE_OPENAI_API_VERSION alongside the existing checks for AZURE_SECRET_KEY and
AZURE_ENDPOINT_URL, throwing an error if it is missing. Update the code to use
this environment variable for the API version, defaulting to "2024-10-21" if not
set. Also, update README.md to document the new AZURE_OPENAI_API_VERSION
environment variable and its default value.


if (!process.env.AZURE_ENDPOINT_URL) {
throw new Error('Missing AZURE_ENDPOINT_URL environment variable');
}
return new AzureOpenAI({
apiKey: process.env.AZURE_SECRET_KEY,
endpoint: process.env.AZURE_ENDPOINT_URL,
apiVersion: '2024-10-21',
});
};

export const openai = new AzureOpenAI({
apiKey: process.env.AZURE_SECRET_KEY,
endpoint: process.env.AZURE_ENDPOINT_URL,
apiVersion: '2024-10-21',
});
export const openai = process.env.AZURE_SECRET_KEY && process.env.AZURE_ENDPOINT_URL
? createAzureOpenAI()
: null;

export const createDirectOpenAI = () => {
if (!process.env.OPENAI_API_KEY) {
Expand Down