I’m encountering a 404 resource not found error when attempting to work with Azure OpenAI and LangChain. The error message indicates {'error': {'code': '404', 'message': 'Resource not found'}}.
Here are the steps I’ve taken to troubleshoot this issue:
Confirmed that the API key, deployment name, model version, and endpoint URL are all accurate.
The model was successfully deployed over 24 hours ago, so it should be operational.
I’ve monitored the logs in Azure, and the requests are indeed reaching the service.
My embeddings model has already been established in Azure, and the embeddings have been generated and stored locally.
Despite following the documentation for both LangChain and Azure OpenAI closely, I still face this error. Below is the code I am using:
import os
import streamlit as st
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import AzureOpenAIEmbeddings
from langchain_openai import AzureOpenAI
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
load_dotenv()
# Environment variables
AZURE_ENDPOINT="https://mycompany-openai.openai.azure.com/"
API_KEY = "my_api_key"
MODEL_DEPLOYMENT = "gpt-35-turbo"
API_VERSION = "2024-02-15-preview"
# Configuration
endpoint_url = os.getenv("AZURE_ENDPOINT")
api_key = os.getenv("API_KEY")
api_version = "2024-02-15-preview"
model_name = os.getenv("MODEL_DEPLOYMENT")
# Load PDF documents
docs_path = './documents'
all_docs = []
for file in os.listdir(docs_path):
if file.endswith('.pdf'):
pdf_path = os.path.join(docs_path, file)
pdf_loader = PyPDFLoader(pdf_path)
pages = pdf_loader.load()
all_docs.extend(pages)
# Setup embeddings
embedding_model = AzureOpenAIEmbeddings(
model="text-embedding-ada-002"
)
# Load existing vector database
vector_db = FAISS.load_local("saved_vectors", embedding_model, allow_dangerous_deserialization=True)
# Initialize language model
ai_model = AzureOpenAI(
api_key=api_key,
api_version=api_version,
deployment_name=model_name,
temperature=0.7
)
# Create prompt
QUESTION_PROMPT = """Based on this information:
{context}
Please answer:
{question}
Response:"""
custom_prompt = PromptTemplate(
template=QUESTION_PROMPT, input_variables=["context", "question"]
)
# Build QA system
qa_system = RetrievalQA.from_chain_type(
llm=ai_model,
chain_type="stuff",
retriever=vector_db.as_retriever(),
chain_type_kwargs={"prompt": custom_prompt}
)
# Streamlit interface
st.title("Document Q&A Bot")
st.write("Ask questions about your documents")
query = st.text_input("Your question:")
if query:
try:
response = qa_system.invoke({"query": query})
output = response['output_text']
except Exception as error:
output = f"Error occurred: {str(error)}"
st.write("Response:", output)
I see the issue. You’re mixing hardcoded values with environment variables, and your Azure endpoint config is probably mismatched.
The main problem is your Azure OpenAI initialization. Your embedding model setup is missing the connection parameters that your main model has.
Honestly though, all these Azure OpenAI config headaches are exactly why I stopped writing custom integration code. Every time Azure updates their API or you switch models, you’re back to debugging connection strings and deployment names.
I handle all these integrations through Latenode now. It’s got native connectors for both Azure OpenAI and LangChain that handle the config automatically. You just drag and drop components, set credentials once, and it manages all the API versioning and endpoint routing.
Built a similar document QA system last month using Latenode’s visual workflow builder. Took 30 minutes instead of days debugging connections. Plus when Azure changes their API again, Latenode updates their connector and my workflow keeps running.
The workflow handles PDF processing, embedding generation, vector storage, and the QA chain all in one visual flow. No more wrestling with config mismatches or dependency conflicts.
Had the same issue with Azure OpenAI through LangChain a few months ago. It wasn’t the main LLM setup - it was my embedding model config that was broken. Your AzureOpenAIEmbeddings is missing the Azure connection parameters. Add the same endpoint and API details:
You’re mixing hardcoded values with environment variables. Double-check that your env file has all the variables you’re trying to load. I’d throw in some debug prints to see what os.getenv() is actually returning before you initialize the models. That 404 might be coming from the embedding model failing during vector operations, not the chat model.
The 404 error is happening because your environment variables aren’t loading properly. You’re defining AZURE_ENDPOINT and other constants at the top but then using os.getenv() - which might return None values.
I hit this same issue when I moved my Azure OpenAI setup to production. Your os.getenv() calls return None if the .env file isn’t in the right spot relative to your script.
Add some debug prints after loading your environment variables:
If any print None, that’s your problem. Either use the hardcoded values you defined at the top, or make sure your .env file is properly formatted and in the same directory as your script. The 404 means Azure can’t find your deployment because it’s getting a malformed endpoint URL or deployment name.
Your azure_openai_endpoint parameter is missing from the AzureOpenAI setup. You’ve got api_key, api_version, and deployment_name but forgot the endpoint URL. Add azure_endpoint=endpoint_url to your ai_model config. Double-check that your deployment name matches exactly what’s in the Azure portal - it’s case sensitive.