I have a Python Flask app deployed on Google Cloud Platform. The app uses Langchain to search through documents using OpenAI embeddings. Everything runs perfectly when I test it on my local machine, but once deployed to GCP, I keep getting openai.APIConnectionError: Connection error when calling the similarity search function.
The error happens specifically when trying to perform the similarity search. Has anyone encountered this issue before? I’m not sure if it’s a network configuration problem with GCP or something else. The same API key works fine locally so that shouldn’t be the issue.
Check your environment variables first. I’ve seen this exact issue when the API key wasn’t set properly in the cloud environment even though it worked locally.
Also verify your GCP service has the right IAM permissions. Sometimes the compute instance doesn’t have internet access or the service account is locked down.
Here’s what usually fixes it - add retry logic and connection pooling to your OpenAI client. Cloud environments are way less forgiving with network hiccups than local setups.
from openai import OpenAI
import time
def get_embeddings_with_retry(text, max_retries=3):
for attempt in range(max_retries):
try:
client = OpenAI(api_key=my_api_key, timeout=60)
return client.embeddings.create(input=text, model="text-embedding-ada-002")
except Exception as e:
if attempt == max_retries - 1:
raise e
time.sleep(2 ** attempt)
This video covers deployment configs that might help:
Check your cloud logs too - they’ll show you exactly where the connection fails. Error messages are usually vague but logs tell the real story.
i think it’s def a network setting. gcp can be tough with security rules. maybe check your firewall and see if outbound traffic is allowed for your app. good luck!
GCP handles SSL certs differently than your local setup. Same thing happened to me with App Engine - worked fine locally, failed in the cloud. OpenAI’s API needs specific SSL verification that breaks in containerized environments sometimes. Set the CURL_CA_BUNDLE environment variable to your cert bundle, or throw in verify=False just to test if it’s SSL causing this. Also check if your GCP region has connectivity issues with OpenAI’s servers - I switched from us-west to us-central once because of routing problems. Your Chroma vector database looks good, so it’s definitely failing on the actual OpenAI API call.
Had this exact problem deploying my langchain app to Cloud Run last month. It’s timeout settings, not firewall rules. Google Cloud’s default timeouts are way shorter than local, and OpenAI embedding calls can drag on longer than expected. Set explicit timeout values in your OpenAI client config and bump up the timeout in your Cloud Run service settings. Also update to the latest openai library - recent versions fixed connection handling issues that mess with cloud deployments.