Skip to content

fix(gateway): log friendly user message in non-production environments if AI_GATEWAY_API_KEY is not configured#11113

Merged
gr2m merged 16 commits intomainfrom
friendly-message-on-gateway-unauthenticated
Dec 12, 2025
Merged

fix(gateway): log friendly user message in non-production environments if AI_GATEWAY_API_KEY is not configured#11113
gr2m merged 16 commits intomainfrom
friendly-message-on-gateway-unauthenticated

Conversation

@gr2m
Copy link
Copy Markdown
Collaborator

@gr2m gr2m commented Dec 11, 2025

Summary

this PR changes

image

to

image

Manual Verification

Tested locally

Checklist

  • (see future work below) Tests have been added / updated (for bug
    fixes / features)
  • n/a Documentation has been added / updated (for bug fixes /
    features)
  • A patch changeset for relevant packages has been added (for bug
    fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Future Work

  • Add tests that cover this error case in local/production
    environments

'Learn more: \u001b[34mhttps://vercel.link/unauthenticated-ai-gateway-v6\u001b[0m\n',
);

process.exit(1);
Copy link
Copy Markdown
Collaborator Author

@gr2m gr2m Dec 12, 2025

Choose a reason for hiding this comment

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

I wonder if we would throw prettyErrorMessage instead of using process.exit() (where prettyErrorMessage is the full string with colors that currently gets logged out). We would still get a pretty message, but in a dev server environment it wouldn't kill the process

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

would look like this

Image

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

alternatively, throwing an error with stack set to ''

image

Copy link
Copy Markdown
Collaborator

@lgrammel lgrammel left a comment

Choose a reason for hiding this comment

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

I think it is important to keep the stack traces. Maybe it can be pruned? a nicer message is great

@gr2m
Copy link
Copy Markdown
Collaborator Author

gr2m commented Dec 12, 2025

For pruning I really like https://github.com/sindresorhus/clean-stack but we won't bring in new dependencies just for that, even if Sindre's modules are small and high quality.

I'd just limit the number of stack lines to 5

@gr2m gr2m marked this pull request as ready for review December 12, 2025 16:50
if (!GatewayAuthenticationError.isInstance(error)) return error;

const isProductionEnv = process?.env.NODE_ENV === 'production';
const moreInfoURL = 'https://vercel.link/unauthenticated-ai-gateway-v6';
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we are adding redirects to ai-sdk.dev right now

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@gr2m gr2m marked this pull request as draft December 12, 2025 16:54
Comment on lines +30 to +36
// prune the stack to 5 lines for clarity
if (devEnvironmentError.stack) {
devEnvironmentError.stack = devEnvironmentError.stack
.split('\n')
.slice(0, 6)
.join('\n');
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hm how would this look like in practice? we do not know how deep the user side stack trace is.

if (!GatewayAuthenticationError.isInstance(error)) return error;

const isProductionEnv = process?.env.NODE_ENV === 'production';
const moreInfoURL = 'http://v6.ai-sdk.dev/unauthenticated-ai-gateway';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@nicoalbanese stable path issue here?

@gr2m gr2m marked this pull request as ready for review December 12, 2025 17:53
if (!GatewayAuthenticationError.isInstance(error)) return error;

const isProductionEnv = process?.env.NODE_ENV === 'production';
const moreInfoURL = 'https://v6.ai-sdk.dev/unauthenticated-ai-gateway';
Copy link
Copy Markdown
Collaborator Author

@gr2m gr2m Dec 12, 2025

Choose a reason for hiding this comment

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

will change to 'https://ai-sdk.dev/unauthenticated-ai-gateway' in backport to v5

@gr2m gr2m disabled auto-merge December 12, 2025 19:17
@gr2m gr2m enabled auto-merge (squash) December 12, 2025 19:17
@gr2m gr2m merged commit 4e2b04d into main Dec 12, 2025
19 checks passed
@gr2m gr2m deleted the friendly-message-on-gateway-unauthenticated branch December 12, 2025 19:18
@gr2m gr2m added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Dec 12, 2025
vercel-ai-sdk bot pushed a commit that referenced this pull request Dec 12, 2025
@vercel-ai-sdk vercel-ai-sdk bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Dec 12, 2025
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Dec 12, 2025

⚠️ Backport to release-v5.0 created but has conflicts: #11137

gr2m added a commit that referenced this pull request Dec 12, 2025
…nvironments if `AI_GATEWAY_API_KEY` is not configured (#11137)

This is an automated backport of #11113 to the release-v5.0 branch.

---------

Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
@gr2m gr2m added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/gateway Issues related to the @ai-sdk/gateway provider and removed ai/gateway labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/gateway Issues related to the @ai-sdk/gateway provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants