-
Notifications
You must be signed in to change notification settings - Fork 306
Added an error message to nudge folks to the new SDK. #461
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
Open
dynamicwebpaige
wants to merge
3
commits into
google-gemini:main
Choose a base branch
from
dynamicwebpaige:update-for-new-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,28 @@ export async function getHeaders(url: RequestUrl): Promise<Headers> { | |
return headers; | ||
} | ||
|
||
/** | ||
* Generates the request options to be passed to the fetch API. | ||
* @param requestOptions - The user-defined request options. | ||
* @returns The generated request options. | ||
*/ | ||
function buildFetchOptions(requestOptions?: SingleRequestOptions): RequestInit { | ||
const fetchOptions = {} as RequestInit; | ||
if (requestOptions?.signal !== undefined || requestOptions?.timeout >= 0) { | ||
const controller = new AbortController(); | ||
if (requestOptions?.timeout >= 0) { | ||
setTimeout(() => controller.abort(), requestOptions.timeout); | ||
} | ||
if (requestOptions?.signal) { | ||
requestOptions.signal.addEventListener("abort", () => { | ||
controller.abort(); | ||
}); | ||
} | ||
fetchOptions.signal = controller.signal; | ||
} | ||
return fetchOptions; | ||
} | ||
|
||
export async function constructModelRequest( | ||
model: string, | ||
task: Task, | ||
|
@@ -196,46 +218,54 @@ async function handleResponseNotOk( | |
response: Response, | ||
url: string, | ||
): Promise<void> { | ||
let message = ""; | ||
let originalMessage = ""; | ||
let errorDetails; | ||
let isUnsupportedModelError = false; | ||
const newSdkUrl = "https://github.com/googleapis/js-genai"; | ||
// Patterns for newer models likely requiring the new SDK | ||
const unsupportedModelPatterns = [/gemini-2/, /gemini-pro-2.0/, /gemini-ultra-2.0/, /gemini-flash-2.5/, /gemini-pro-2.5/, /gemini-2.5-pro/, /gemini-2.5-pro-exp/, /gemini-2.0-flash/]; | ||
|
||
// Extract model name from the URL | ||
let requestedModel = "unknown"; | ||
// Match /v.../models/model-name: or /v.../tunedModels/model-name: | ||
const modelNameMatch = url.match(/\/(v\d+(?:beta)?\d*)\/(?:models|tunedModels)\/([^:]+):/); | ||
if (modelNameMatch && modelNameMatch[2]) { | ||
requestedModel = modelNameMatch[2]; | ||
} | ||
|
||
try { | ||
const json = await response.json(); | ||
message = json.error.message; | ||
if (json.error.details) { | ||
message += ` ${JSON.stringify(json.error.details)}`; | ||
errorDetails = json.error.details; | ||
originalMessage = json.error?.message || ""; // Safely access message | ||
errorDetails = json.error?.details; // Safely access details | ||
|
||
// Check 1: Explicit model name check | ||
if (unsupportedModelPatterns.some(pattern => pattern.test(requestedModel))) { | ||
// If the requested model *is* one of the new ones, assume this error is due to SDK incompatibility. | ||
isUnsupportedModelError = true; | ||
} | ||
|
||
// Append original details to message if they exist and aren't already in the message | ||
if (errorDetails && !originalMessage.includes(JSON.stringify(errorDetails))) { | ||
originalMessage += ` ${JSON.stringify(errorDetails)}`; | ||
} | ||
|
||
} catch (e) { | ||
// ignored | ||
// Error parsing JSON is ignored, proceed with original status/text | ||
} | ||
|
||
let finalMessage = `Error fetching from ${url.toString()}: [${response.status} ${response.statusText}] ${originalMessage || 'Server responded with an error.'}`; // Provide default if message is empty | ||
|
||
if (isUnsupportedModelError) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be a breaking change if user has client side logic to process the |
||
// Prepend the specific message for known unsupported models | ||
finalMessage = `It looks like you're trying to use a newer Gemini model (${requestedModel}) with an older SDK. This model requires the latest SDK. Please upgrade to the latest SDK available at ${newSdkUrl}. Original error: ${finalMessage}`; | ||
} else if ((response.status === 400 || response.status === 404) && (originalMessage.includes("Invalid model") || originalMessage.includes("not found") || originalMessage.includes("API key not valid"))) { | ||
// Add a more general hint for other model-related or key errors that might be due to trying new models/regions | ||
finalMessage += `\nHint: If you are trying to use a newer Gemini model (e.g., Gemini 2.0, 2.5 Pro) or a specific region, ensure your API key is valid for that model/region and consider upgrading to the latest SDK: ${newSdkUrl}`; | ||
} | ||
|
||
throw new GoogleGenerativeAIFetchError( | ||
`Error fetching from ${url.toString()}: [${response.status} ${ | ||
response.statusText | ||
}] ${message}`, | ||
finalMessage, | ||
response.status, | ||
response.statusText, | ||
errorDetails, | ||
); | ||
} | ||
|
||
/** | ||
* Generates the request options to be passed to the fetch API. | ||
* @param requestOptions - The user-defined request options. | ||
* @returns The generated request options. | ||
*/ | ||
function buildFetchOptions(requestOptions?: SingleRequestOptions): RequestInit { | ||
const fetchOptions = {} as RequestInit; | ||
if (requestOptions?.signal !== undefined || requestOptions?.timeout >= 0) { | ||
const controller = new AbortController(); | ||
if (requestOptions?.timeout >= 0) { | ||
setTimeout(() => controller.abort(), requestOptions.timeout); | ||
} | ||
if (requestOptions?.signal) { | ||
requestOptions.signal.addEventListener("abort", () => { | ||
controller.abort(); | ||
}); | ||
} | ||
fetchOptions.signal = controller.signal; | ||
} | ||
return fetchOptions; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use a much stronger message similar to the Python package, see https://github.com/google-gemini/deprecated-generative-ai-python.
For older models, we want to encourage people to use the new SDK. This SDK will be around not to break the existing users. New comers to the old models should also use the new SDK ;)