The issue stems from the underlying coding agent Pi not properly supporting Gemini API endpoint with OpenAI compatibility. You'll see 400 errors with no details.
- Create new AI preset (Settings > AI > Create Preset)
- Click 'Custom'
- Add
https://generativelanguage.googleapis.com/v1beta/openaifor the 'Custom URL' - Add your API key and choose your model
Note
We are using .../openai to pass the connection test in the UI. See below for the actual URL the Pi agent will use.
- Open
~/node_modules/@mariozechner/pi-coding-agent/dist/core/model-registry.js(it should've been auto-installed via bun when you first open the app) - Apply the following patch
// Separate providers into "full replacement" (has models) vs "override-only" (no models)
const replacedProviders = new Set();
const overrides = new Map();
for (const [providerName, providerConfig] of Object.entries(config.providers)) {
+ if (providerConfig.baseUrl.includes("generativelanguage.googleapis.com/v1beta/openai")) {
+ providerConfig.baseUrl = providerConfig.baseUrl.replace(/\/openai/, '') // remove OpenAI compat
+ providerConfig.api = "google-generative-ai"; // override api type
+ }
if (providerConfig.models && providerConfig.models.length > 0) {
// Has custom models -> full replacement
replacedProviders.add(providerName);
}
...Note
This patch will make Pi to use google-generative-ai with generativelanguage.googleapis.com/v1beta, instead of openai-completions with generativelanguage.googleapis.com/v1beta/openai which has a lot of quirks that will result in 400 errors.
See this docs for list of apis supported by Pi.