{"id":7251,"library":"g4f","title":"G4F (GPT4Free)","description":"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.","status":"active","version":"7.4.7","language":"en","source_language":"en","source_url":"https://github.com/xtekky/gpt4free","tags":["AI","LLM","GPT","Free API","Chatbot","Generative AI","Multi-provider","Unofficial API"],"install":[{"cmd":"pip install -U g4f","lang":"bash","label":"Basic Installation"},{"cmd":"pip install -U g4f[all]","lang":"bash","label":"Full Installation (includes GUI, API, image, webdriver dependencies)"}],"dependencies":[{"reason":"Required for running the library. Python 3.10+ is recommended.","package":"Python","optional":false},{"reason":"Required for providers that utilize browser automation for access.","package":"Google Chrome/Chromium","optional":true}],"imports":[{"note":"The Client API (g4f.client.Client) is the recommended and modern approach for OpenAI-compatible interactions, offering better control and feature access. Direct use of g4f.ChatCompletion.create is still supported but less flexible.","wrong":"import g4f; response = g4f.ChatCompletion.create(...)","symbol":"Client","correct":"from g4f.client import Client"},{"note":"Individual providers are imported from g4f.Provider for explicit selection, e.g., g4f.Provider.Bing, g4f.Provider.OpenaiChat.","wrong":null,"symbol":"Provider","correct":"from g4f.Provider import <ProviderName>"}],"quickstart":{"code":"import g4f\nfrom g4f.client import Client\n\n# Initialize the client. For some providers, additional setup (like a browser executable path) might be needed.\n# client = Client(provider=g4f.Provider.Bing, browser_executable_path=\"/path/to/chrome\")\nclient = Client()\n\n# Define the conversation messages\nmessages = [\n    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n    {\"role\": \"user\", \"content\": \"Hello, how are you today?\"}\n]\n\ntry:\n    # Create a chat completion\n    response = client.chat.completions.create(\n        model=\"gpt-3.5-turbo\", # You can specify other models like 'gpt-4o-mini' if available\n        messages=messages,\n        web_search=False # Set to True to enable web search if supported by the provider\n    )\n\n    # Print the response\n    if response.choices:\n        print(response.choices[0].message.content)\n    else:\n        print(\"No response received from the model.\")\n\nexcept g4f.errors.RateLimitError:\n    print(\"Rate limit exceeded. Please try again later or switch providers.\")\nexcept g4f.errors.RetryProviderError as e:\n    print(f\"Provider error: {e}. Consider trying a different provider or model.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to use the `g4f.client.Client` to interact with a language model, sending a simple chat message and printing the response. It includes basic error handling for common `g4f` exceptions. Users can explicitly select providers or rely on the library's default/auto-selection."},"warnings":[{"fix":"Use at your own risk for personal experimentation or educational purposes only. For production, always opt for official APIs with proper authentication and licensing.","message":"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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Implement robust error handling (e.g., try-except for `g4f.errors.RetryProviderError`, `g4f.errors.RateLimitError`), use `g4f.Provider.RetryProvider` with a list of backup providers, or regularly check community resources like 'g4f-working' for status updates.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure the environment where `g4f` is run has an active browser session with the required cookies, or configure the client to manually pass cookies if applicable for the chosen provider.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always keep the `g4f` library updated using `pip install -U g4f`. If warnings persist, `import warnings; warnings.filterwarnings(\"ignore\")` can suppress them, but updating is generally preferred.","message":"Older versions of the library or specific provider implementations might trigger version deprecation warnings or cease to function as underlying third-party APIs change.","severity":"deprecated","affected_versions":"< 7.0.0 (and potentially specific older minor releases)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Wait for the rate limit to reset, try a different `g4f.Provider`, or use `g4f.Provider.RetryProvider` to automatically cycle through available providers.","cause":"The chosen provider has hit its usage limits for your IP address or session.","error":"g4f.errors.RateLimitError: Response 402: Rate limit"},{"fix":"Ensure 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.","cause":"All selected or default providers failed to respond or encountered an error, often due to being offline, blocked, or having unexpected changes.","error":"g4f.errors.RetryProviderError: No providers available (All providers failed)."},{"fix":"Consult 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.","cause":"The specified model is not supported by the chosen provider, or the provider has a limited set of available models.","error":"Model not found for text completion (e.g., trying to use 'gpt-4o' with a provider that only supports 'gpt-3.5-turbo')"},{"fix":"While 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.","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.","error":"UserWarning: Curlm already closed! quitting from process_data"}]}