Skip to content

feat: adding AI Gateway support#1627

Open
Kylejeong2 wants to merge 4 commits intomainfrom
kylejeong/stg-1225-adding-vercel-ai-gateway-support-to-stagehand
Open

feat: adding AI Gateway support#1627
Kylejeong2 wants to merge 4 commits intomainfrom
kylejeong/stg-1225-adding-vercel-ai-gateway-support-to-stagehand

Conversation

@Kylejeong2
Copy link
Member

@Kylejeong2 Kylejeong2 commented Jan 28, 2026

why

New open source models are released, and don't necessarily have a great inference provider at all times. Seeing as we're already using the AI-SDK, it makes sense to add support for the AI Gateway.

what changed

Added Vercel AI Gateway as a provider to the llm providers list.

test plan

Testing with Evals


Summary by cubic

Adds Vercel AI Gateway as a provider so model calls can route through it, making it easier to support new/open-source models. Enforces JSON structured outputs (and temperature=1) for Kimi and DeepSeek models, and fixes eval model name parsing.

Written for commit 42a1c50. Summary will update on new commits. Review in cubic

@changeset-bot
Copy link

changeset-bot bot commented Jan 28, 2026

⚠️ No Changeset found

Latest commit: 42a1c50

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 28, 2026

Greptile Overview

Greptile Summary

This PR adds Vercel AI Gateway support to enable routing model calls through the gateway, along with model-specific fixes for Kimi and DeepSeek.

Key changes:

  • Added gateway provider to AISDKProviders and AISDKProvidersWithAPIKey for AI Gateway routing
  • Enforced temperature=1 for Kimi models (API constraint)
  • Added explicit JSON schema prompts for DeepSeek and Kimi models to ensure structured output compliance
  • Fixed model name parsing in evals to handle multiple slashes correctly using substring instead of split
  • Mirrored changes in both core AISdkClient and eval AISdkClientWrapped for consistency

Concerns:

  • The gateway import from "ai" package may need verification - check if it should be imported from "@ai-sdk/gateway" instead

Confidence Score: 3/5

  • Safe to merge after verifying the gateway import source
  • The changes are well-structured and consistent across files. The main concern is the potentially incorrect import source for gateway/createGateway which could cause runtime issues. Once verified, this should be safe to merge.
  • packages/core/lib/v3/llm/LLMProvider.ts - verify gateway import source

Important Files Changed

Filename Overview
packages/core/lib/v3/llm/LLMProvider.ts Adds AI Gateway support by importing gateway and createGateway from 'ai' package, but import source may be incorrect
packages/core/lib/v3/llm/aisdk.ts Adds Kimi model support with temperature=1 constraint and forces JSON output for DeepSeek/Kimi models
packages/core/package.json Adds @ai-sdk/gateway ^3.0.25 as optional dependency

Sequence Diagram

sequenceDiagram
    participant User
    participant LLMProvider
    participant AISdkClient
    participant AIGateway
    participant Model

    User->>LLMProvider: getClient(modelName)
    
    alt modelName contains "/"
        LLMProvider->>LLMProvider: Parse provider/model (substring)
        LLMProvider->>LLMProvider: Check if provider is "gateway"
        
        alt provider is "gateway"
            LLMProvider->>AIGateway: createGateway(clientOptions)
            AIGateway-->>LLMProvider: gateway provider instance
        end
        
        LLMProvider->>AISdkClient: new AISdkClient(model)
        AISdkClient-->>LLMProvider: client instance
    end
    
    User->>AISdkClient: createChatCompletion(options)
    
    alt model is Kimi
        AISdkClient->>AISdkClient: Force temperature=1
    end
    
    alt response_model exists AND (DeepSeek OR Kimi)
        AISdkClient->>AISdkClient: Append JSON schema instruction
    end
    
    AISdkClient->>Model: generateObject/generateText()
    Model-->>AISdkClient: response
    AISdkClient-->>User: formatted response
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

No files reviewed, no comments

Edit Code Review Agent Settings | Greptile

cubic-dev-ai[bot]

This comment was marked as resolved.

@Kylejeong2 Kylejeong2 marked this pull request as draft January 28, 2026 20:38
@miguelg719
Copy link
Collaborator

@Kylejeong2 would users have to define gateway/openai/gpt-5 as the model string?

@Kylejeong2
Copy link
Member Author

@Kylejeong2 would users have to define gateway/openai/gpt-5 as the model string?

Yeah that is the current flow, but I can update it to make more ergonomic

@Kylejeong2 Kylejeong2 marked this pull request as ready for review February 1, 2026 00:04
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

import { deepseek, createDeepSeek } from "@ai-sdk/deepseek";
import { perplexity, createPerplexity } from "@ai-sdk/perplexity";
import { ollama, createOllama } from "ollama-ai-provider-v2";
import { gateway, createGateway } from "ai";
Copy link
Contributor

Choose a reason for hiding this comment

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

The import source may be incorrect. According to the lockfile, the ai package (v5.0.76) includes @ai-sdk/gateway 2.0.0, but gateway and createGateway may not be re-exported from the main ai package. Check if these should be imported from @ai-sdk/gateway instead:

Suggested change
import { gateway, createGateway } from "ai";
import { gateway, createGateway } from "@ai-sdk/gateway";
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/lib/v3/llm/LLMProvider.ts
Line: 34:34

Comment:
The import source may be incorrect. According to the lockfile, the `ai` package (v5.0.76) includes `@ai-sdk/gateway` 2.0.0, but `gateway` and `createGateway` may not be re-exported from the main `ai` package. Check if these should be imported from `@ai-sdk/gateway` instead:

```suggestion
import { gateway, createGateway } from "@ai-sdk/gateway";
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Collaborator

Choose a reason for hiding this comment

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

incorrect ai is the correct package

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 6 files

Confidence score: 5/5

  • Only minor wording clarity issue in packages/evals/lib/AISdkClientWrapped.ts; it doesn’t affect runtime behavior and is low severity.
  • Score reflects low risk since the concern is documentation/terminology rather than functional change.
  • Pay close attention to packages/evals/lib/AISdkClientWrapped.ts - update the prompt wording to say JSON schema format for clarity.
Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="packages/evals/lib/AISdkClientWrapped.ts">

<violation number="1" location="packages/evals/lib/AISdkClientWrapped.ts:153">
P2: Misleading terminology: the prompt says 'zod schema format' but `toJsonSchema()` produces a JSON Schema, not a Zod schema. Consider changing the wording to 'JSON schema format' for clarity.</violation>
</file>
Architecture diagram
sequenceDiagram
    participant App as App / Eval Engine
    participant LLMP as LLMProvider.ts
    participant Client as AISdkClient
    participant AISDK as Vercel AI SDK
    participant GW as NEW: AI Gateway
    participant Provider as Model Provider (Kimi/DeepSeek)

    Note over App,Provider: Runtime Request Flow with AI Gateway & Model Optimization

    App->>LLMP: getAISDKLanguageModel(provider, model)
    alt NEW: Provider is "gateway"
        LLMP->>AISDK: createGateway(config)
    else Standard Provider
        LLMP->>AISDK: createProvider(config)
    end
    LLMP-->>App: languageModel instance

    App->>Client: createChatCompletion(options)
    
    rect rgb(23, 37, 84)
    Note right of Client: CHANGED: Model-specific Logic
    opt modelId contains "kimi"
        Client->>Client: NEW: Force temperature = 1
    end

    opt modelId contains "deepseek" OR "kimi"
        Client->>Client: NEW: Inject Zod Schema & JSON instructions into prompt
    end
    end

    alt generateObject (Structured Data)
        Client->>AISDK: generateObject(model, schema, messages)
        AISDK->>GW: Request with Gateway Headers
        GW->>Provider: Route to Inference API
        Provider-->>GW: JSON Response
        GW-->>AISDK: Relay Response
        AISDK-->>Client: Typed Object
    else generateText
        Client->>AISDK: generateText(model, messages)
        AISDK->>GW: Request with Gateway Headers
        GW->>Provider: Route to Inference API
        Provider-->>GW: Text Response
        GW-->>AISDK: Relay Response
        AISDK-->>Client: Text
    end

    Client-->>App: LLMResponse
Loading

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.


formattedMessages.push({
role: "user",
content: `Respond in this zod schema format:\n${parsedSchema}\n
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Feb 1, 2026

Choose a reason for hiding this comment

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

P2: Misleading terminology: the prompt says 'zod schema format' but toJsonSchema() produces a JSON Schema, not a Zod schema. Consider changing the wording to 'JSON schema format' for clarity.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/evals/lib/AISdkClientWrapped.ts, line 153:

<comment>Misleading terminology: the prompt says 'zod schema format' but `toJsonSchema()` produces a JSON Schema, not a Zod schema. Consider changing the wording to 'JSON schema format' for clarity.</comment>

<file context>
@@ -136,13 +137,30 @@ export class AISdkClientWrapped extends LLMClient {
+
+        formattedMessages.push({
+          role: "user",
+          content: `Respond in this zod schema format:\n${parsedSchema}\n
+You must respond in JSON format. respond WITH JSON. Do not include any other text, formatting or markdown in your output. Do not include \`\`\` or \`\`\`json in your response. Only the JSON object itself.`,
+        });
</file context>
Suggested change
content: `Respond in this zod schema format:\n${parsedSchema}\n
content: `Respond in this JSON schema format:\n${parsedSchema}\n
Fix with Cubic

@miguelg719
Copy link
Collaborator

@Kylejeong2 would users have to define gateway/openai/gpt-5 as the model string?

Yeah that is the current flow, but I can update it to make more ergonomic

That's fine let's just cover it in the docs!

@Kylejeong2
Copy link
Member Author

it

Yeah I can add into docs in separate PR, can you approve?

Comment on lines 67 to 68
"@ai-sdk/gateway": "^3.0.25",
"@ai-sdk/google": "^2.0.23",
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this the right version? usually v3 ai sdk packages = only works with v6

Copy link
Member Author

Choose a reason for hiding this comment

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

im thinking about removing, since the actual package that we're pulling from is "ai" not the /gateway i added this per cubic's recommendation

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines 65 to 67
"@ai-sdk/cerebras": "^1.0.25",
"@ai-sdk/deepseek": "^1.0.23",
"@ai-sdk/gateway": "^3.0.25",
Copy link
Collaborator

Choose a reason for hiding this comment

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

believe we can also just directly import gateway from ai without needing to add a new dependency

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah removing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants