{"record":{"id":"8ad71641ab373494","repo":"zed-industries/zed","slug":"openai-api-key-environment-variable-not-set","errorCode":null,"errorMessage":"OPENAI_API_KEY environment variable not set","messagePattern":"OPENAI_API_KEY environment variable not set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/edit_prediction_cli/src/openai_client.rs","lineNumber":26,"sourceCode":"use reqwest_client::ReqwestClient;\nuse sqlez::bindable::Bind;\nuse sqlez::bindable::StaticColumnCount;\nuse sqlez_macros::sql;\nuse std::hash::Hash;\nuse std::hash::Hasher;\nuse std::path::Path;\nuse std::sync::{Arc, Mutex};\n\npub struct PlainOpenAiClient {\n    pub http_client: Arc<dyn HttpClient>,\n    pub api_key: String,\n}\n\nimpl PlainOpenAiClient {\n    pub fn new() -> Result<Self> {\n        let http_client: Arc<dyn http_client::HttpClient> = Arc::new(ReqwestClient::new());\n        let api_key = std::env::var(\"OPENAI_API_KEY\")\n            .map_err(|_| anyhow::anyhow!(\"OPENAI_API_KEY environment variable not set\"))?;\n        Ok(Self {\n            http_client,\n            api_key,\n        })\n    }\n\n    pub async fn generate(\n        &self,\n        model: &str,\n        max_tokens: u64,\n        messages: Vec<RequestMessage>,\n    ) -> Result<OpenAiResponse> {\n        let request = OpenAiRequest {\n            model: model.to_string(),\n            messages,\n            stream: false,\n            stream_options: None,\n            max_completion_tokens: Some(max_tokens),","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/edit_prediction_cli/src/openai_client.rs#L8-L44","documentation":"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.","triggerScenarios":"Thrown at crates/edit_prediction_cli/src/openai_client.rs:26 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Export OPENAI_API_KEY with a valid key before running the tool","Add the variable to the shell profile or environment of the CLI"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}