G4F (GPT4Free)
GPT4Free (g4f) is an open-source Python library that provides access to a collection of powerful language models and media-generation models through various third-party providers. It aims to offer multi-provider support, a local GUI, OpenAI-compatible REST APIs, and convenient Python and JavaScript clients, often without requiring official API keys. The library is under active development with frequent releases, currently at version 7.4.7.
Common errors
-
g4f.errors.RateLimitError: Response 402: Rate limit
cause The chosen provider has hit its usage limits for your IP address or session.fixWait for the rate limit to reset, try a different `g4f.Provider`, or use `g4f.Provider.RetryProvider` to automatically cycle through available providers. -
g4f.errors.RetryProviderError: No providers available (All providers failed).
cause All selected or default providers failed to respond or encountered an error, often due to being offline, blocked, or having unexpected changes.fixEnsure you have a stable internet connection. Try explicitly specifying a known working provider (e.g., `client = Client(provider=g4f.Provider.Bing)`). Check the `g4f-working` project for currently active providers. -
Model not found for text completion (e.g., trying to use 'gpt-4o' with a provider that only supports 'gpt-3.5-turbo')
cause The specified model is not supported by the chosen provider, or the provider has a limited set of available models.fixConsult the `g4f` documentation or `g4f.Provider` modules to identify which models are supported by each provider. Experiment with common models like 'gpt-3.5-turbo' or 'gpt-4o-mini' which tend to have broader support. -
UserWarning: Curlm already closed! quitting from process_data
cause This warning often appears in conjunction with network or provider-related errors, indicating an issue with the underlying cURL library's handling of HTTP requests, particularly during asynchronous operations.fixWhile often a symptom rather than a primary cause, addressing the root provider error (e.g., changing providers, ensuring stable connection) usually resolves this. It can sometimes be safely ignored if the main functionality still works.
Warnings
- breaking G4F operates by routing requests through third-party websites, potentially violating their Terms of Service. This practice can lead to legal and ethical issues, making it unsuitable for production environments or handling sensitive/confidential data.
- gotcha Provider instability is common; individual providers may go offline, change their behavior, or introduce rate limits without warning. This can lead to frequent errors or unexpected downtime.
- gotcha Some providers require active user sessions and cookies (e.g., from a logged-in browser). While `g4f` attempts to retrieve these automatically in local environments, manual provision might be necessary in non-local deployments.
- deprecated Older versions of the library or specific provider implementations might trigger version deprecation warnings or cease to function as underlying third-party APIs change.
Install
-
pip install -U g4f -
pip install -U g4f[all]
Imports
- Client
import g4f; response = g4f.ChatCompletion.create(...)
from g4f.client import Client
- Provider
from g4f.Provider import <ProviderName>
Quickstart
import g4f
from g4f.client import Client
# Initialize the client. For some providers, additional setup (like a browser executable path) might be needed.
# client = Client(provider=g4f.Provider.Bing, browser_executable_path="/path/to/chrome")
client = Client()
# Define the conversation messages
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how are you today?"}
]
try:
# Create a chat completion
response = client.chat.completions.create(
model="gpt-3.5-turbo", # You can specify other models like 'gpt-4o-mini' if available
messages=messages,
web_search=False # Set to True to enable web search if supported by the provider
)
# Print the response
if response.choices:
print(response.choices[0].message.content)
else:
print("No response received from the model.")
except g4f.errors.RateLimitError:
print("Rate limit exceeded. Please try again later or switch providers.")
except g4f.errors.RetryProviderError as e:
print(f"Provider error: {e}. Consider trying a different provider or model.")
except Exception as e:
print(f"An unexpected error occurred: {e}")