zed-industries/zed · error

OPENAI_API_KEY environment variable not set

Error message

OPENAI_API_KEY environment variable not set

What it means

PlainOpenAiClient::new reads the OPENAI_API_KEY environment variable to authenticate; std::env::var failed (unset or non-unicode), so the client cannot be constructed. The missing environment variable is the input at fault.

Source

Thrown at crates/edit_prediction_cli/src/openai_client.rs:26

use reqwest_client::ReqwestClient;
use sqlez::bindable::Bind;
use sqlez::bindable::StaticColumnCount;
use sqlez_macros::sql;
use std::hash::Hash;
use std::hash::Hasher;
use std::path::Path;
use std::sync::{Arc, Mutex};

pub struct PlainOpenAiClient {
    pub http_client: Arc<dyn HttpClient>,
    pub api_key: String,
}

impl PlainOpenAiClient {
    pub fn new() -> Result<Self> {
        let http_client: Arc<dyn http_client::HttpClient> = Arc::new(ReqwestClient::new());
        let api_key = std::env::var("OPENAI_API_KEY")
            .map_err(|_| anyhow::anyhow!("OPENAI_API_KEY environment variable not set"))?;
        Ok(Self {
            http_client,
            api_key,
        })
    }

    pub async fn generate(
        &self,
        model: &str,
        max_tokens: u64,
        messages: Vec<RequestMessage>,
    ) -> Result<OpenAiResponse> {
        let request = OpenAiRequest {
            model: model.to_string(),
            messages,
            stream: false,
            stream_options: None,
            max_completion_tokens: Some(max_tokens),

View on GitHub (pinned to f4178619ac)

Solutions

  1. Export OPENAI_API_KEY with a valid key before running the tool
  2. Add the variable to the shell profile or environment of the CLI
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/edit_prediction_cli/src/openai_client.rs:26 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/8ad71641ab373494. Report an issue: GitHub.