How to Use gpt4free with LangChain: Complete Integration Guide
Yes, gpt4free provides a native LangChain integration via the ChatAI class, which inherits from langchain_community.chat_models.openai.ChatOpenAI and routes all requests through the g4f client to free LLM providers.
The gpt4free LangChain integration allows you to swap expensive API calls with free provider alternatives while keeping your existing LangChain chains, agents, and streaming logic intact. The integration lives in the g4f.integration.langchain module and handles message conversion, provider selection, and async execution automatically.
How the gpt4free LangChain Integration Works
Core Components
The integration consists of three primary components that bridge LangChain's abstraction layer with gpt4free's provider ecosystem:
| Component | File Path | Purpose |
|---|---|---|
| ChatAI | g4f/integration/langchain.py |
LangChain-compatible chat model that wraps g4f clients |
| Client / AsyncClient | g4f/client/__init__.py |
Handles provider routing, retries, and response formatting |
| ChatCompletionMessage | g4f/client/stubs.py |
Pydantic model that translates LangChain messages to OpenAI-compatible format |
Message Flow Architecture
When you invoke a LangChain chain using ChatAI, the following sequence occurs:
-
Validation: The
validate_environmentmethod inChatAIextractsproviderfrommodel_kwargsand instantiates bothg4f.Clientandg4f.AsyncClient. -
Conversion: LangChain
BaseMessageobjects are converted toChatCompletionMessagestubs using a monkey-patchedopenai.convert_message_to_dictfunction defined ing4f/integration/langchain.py. -
Execution: The g4f client selects the specified provider (e.g., Gemini, Claude, Perplexity) and handles the HTTP request, retries, and error handling.
-
Response: The provider response is wrapped in a LangChain
AIMessageand returned to your chain.
Setting Up gpt4free with LangChain
Installation
Ensure you have both packages installed:
pip install g4f langchain langchain-community
Basic Synchronous Usage
Import ChatAI from the integration module and specify your desired provider via model_kwargs:
from g4f.integration.langchain import ChatAI
from langchain.schema import HumanMessage
# Initialize with any free provider supported by g4f
chat = ChatAI(
model="gpt-4o", # Placeholder for logging; actual model determined by provider
model_kwargs={"provider": "Gemini"} # Select Gemini, Claude, Perplexity, etc.
)
response = chat.invoke([HumanMessage(content="Explain quantum computing in one sentence.")])
print(response.content)
The model parameter is used only for identification; the actual LLM serving the request is determined by the provider value.
Advanced gpt4free LangChain Patterns
Streaming Responses
The integration supports LangChain's streaming interface, yielding tokens as they arrive from the provider:
from g4f.integration.langchain import ChatAI
from langchain.schema import HumanMessage
chat = ChatAI(
model="gpt-4o",
model_kwargs={"provider": "Claude"}
)
# Stream tokens in real-time
for chunk in chat.stream([HumanMessage(content="Write a haiku about Python programming.")]):
print(chunk.content, end="", flush=True)
Building LLM Chains
Use ChatAI within standard LangChain chains and prompt templates:
from g4f.integration.langchain import ChatAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
template = """You are an expert coding assistant.
User: {question}
Assistant:"""
prompt = PromptTemplate.from_template(template)
llm = ChatAI(
model="gpt-4o",
model_kwargs={"provider": "Perplexity"}
)
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run({"question": "What is the difference between a list and a tuple in Python?"})
print(result)
Async Execution
For high-concurrency applications, the integration automatically creates an AsyncClient instance accessible via agenerate and astream:
import asyncio
from g4f.integration.langchain import ChatAI
from langchain.schema import HumanMessage
async def main():
chat = ChatAI(
model="gpt-4o",
model_kwargs={"provider": "DeepInfra"}
)
response = await chat.agenerate([
[HumanMessage(content="Summarize the theory of relativity in 50 words.")]
])
print(response.generations[0][0].text)
asyncio.run(main())
Key Implementation Files
Understanding the source structure helps with debugging and extending the integration:
| File | Description | Link |
|---|---|---|
g4f/integration/langchain.py |
Contains ChatAI class, environment validation, and message conversion logic. |
View on GitHub |
g4f/client/__init__.py |
Implements Client and AsyncClient classes that manage provider selection and request execution. |
View on GitHub |
g4f/client/stubs.py |
Defines Pydantic models like ChatCompletionMessage that bridge g4f responses with LangChain expectations. |
View on GitHub |
g4f/Provider/ |
Directory containing individual provider implementations (e.g., Gemini.py, Claude.py). |
View on GitHub |
Summary
- gpt4free LangChain integration is available via
g4f.integration.langchain.ChatAI, which inherits from LangChain'sChatOpenAIclass. - The integration routes all requests through g4f.Client or AsyncClient, enabling access to dozens of free providers by specifying
model_kwargs={"provider": "ProviderName"}. - Message conversion is handled automatically via monkey-patched OpenAI utilities in
g4f/integration/langchain.py, ensuring compatibility with LangChain chains, agents, and streaming interfaces. - Both synchronous and asynchronous execution patterns are supported, making the integration suitable for production web applications and batch processing pipelines.
Frequently Asked Questions
Can I use any gpt4free provider with LangChain?
Yes. The ChatAI class accepts any provider name available in the g4f ecosystem through the model_kwargs={"provider": "ProviderName"} parameter. This includes providers like Gemini, Claude, Perplexity, and DeepInfra. If a provider supports the OpenAI-compatible chat completion interface, it will work seamlessly with LangChain chains.
Does gpt4free LangChain integration support streaming?
Yes. The integration implements LangChain's standard stream method, yielding ChatCompletionChunk objects as tokens arrive from the provider. You can use the same streaming patterns you would use with OpenAI's official LangChain integration, making it a drop-in replacement for real-time text generation applications.
Is the gpt4free LangChain wrapper compatible with async operations?
Yes. The ChatAI class automatically instantiates both synchronous (Client) and asynchronous (AsyncClient) g4f clients during initialization. You can use agenerate and astream methods for non-blocking execution, which is essential for high-concurrency applications built with FastAPI, asyncio, or similar async frameworks.
How do I handle authentication for providers that require API keys?
You can pass API keys through the standard api_key parameter when initializing ChatAI, or include them in model_kwargs. During the validate_environment method, the integration extracts these values and passes them to the underlying g4f.Client. This allows you to use providers that require authentication while maintaining the same LangChain interface.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →