Troubleshooting OpenAI API Timeouts and LangChain Version Conflicts
Resolving HTTPS Timeout Errors
When calling OpenAI endpoints, you may encounter HTTPSConnectionPool timeout exceptions. A reliable fix is to pin urllib3 to an earlier stable release:
pip install urllib3==1.25.11
On Windows 11 with Python 3.9.18, running openai==1.12.0 alongside urllib3==1.25.11 typically restores connectivity without further workarounds.
LangChain Python Version Requirements
LangChain v0.1 and later require Python 3.8 or newer. Attempting to install recent packages on Python 3.7 will fail with dependency resolution errors.
OpenAI SDK Compatibility Issues
Modern LangChain integrations depend on openai>=1.10.0. Instantiate chat models using the current package path:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4", temperature=0)
If network constraints force you to downgrade the OpenAI SDK to 0.28.0, langchain-openai>=0.0.6 will refuse to load because it strictly requires openai<2.0.0,>=1.10.0. As a fallback, import from the community compatibility layer:
from langchain_community.chat_models import ChatOpenAI
chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
This legacy import may raise deprecation warnings but remains functional when newer SDK versions cannot be used.
Prompt Conrtacts for Document Chains
When assembling RAG pipelines with create_stuff_documents_chain and create_retrieval_chain, the prompt must declare specific variable names.
For the document combination step, always include a {context} placeholder. LangChain injects retrieved doccuments into this slot:
from langchain_core.prompts import ChatPromptTemplate
template = """
Answer strictly from the context below. If the information is missing, state that you do not know.
Context:
{context}
User Query:
{input}
"""
prompt = ChatPromptTemplate.from_template(template)
The retrieval wrapper expects the incoming dictionary to carry the user message under the exact key input. Internally, the retriever extracst the query via a mapping step such as:
retrieval_step = (lambda payload: payload["input"]) | retriever
Omitting {input} from the template or passing the query under an alternative key (for example, question) triggers a KeyError. Invoke the chain with the required key:
rag_chain = create_retrieval_chain(retriever, document_chain)
response = rag_chain.invoke({"input": "Explain the refund policy."})