Last reviewed: November 10, 2026. Primary references fetched November 10 UTC; implementation baseline: Gradio 6.31.0.

Direct answer

Connect Gradio to CometAPI with gr.load_chat, supplying CometAPI’s documented base URL , a current text Chat Completions model ID as model, and your server-held CometAPI key as token. Set file_types=None for text input, start with streaming=False, and launch explicitly on 127.0.0.1 with public sharing disabled.

The Gradio 6.31.0 loader implementation builds a ChatInterface around an OpenAI-compatible Chat Completions client. The CometAPI chat reference documents the matching base URL and /v1/chat/completions operation. This gives you a browser conversation without writing the request adapter or managing the chat display yourself.

Who this is for

Use this approach for a local development chat, a small model evaluation, or an internal prototype operated from your own machine. It suits developers who already understand Python virtual environments and can supply process environment variables without embedding credentials in source code.

The example fixes Gradio at 6.31.0, the version displayed by the official references checked for this article. Use a Python interpreter supported by that release. The code below is an implementation recipe and the checks are instructions to perform in your installation; no authenticated CometAPI request was performed for this article.

If you prefer Streamlit’s application model, the Streamlit chat tutorial covers that separate integration. Here the focus is Gradio’s existing loader and the controls it actually exposes.

Key takeaways

  • Pass the provider base URL ending in /v1; the loader supplies the Chat Completions operation.
  • Read the key and model in Python on the server. Keep credentials out of interface components and committed files.
  • Disable uploads explicitly: the loader’s default permits text-encoded files.
  • Treat loader arguments, ChatInterface settings, and launch settings as three separate configuration boundaries.
  • Use a custom callback when you need explicit request timeouts, output limits, retry policy, or strict completion acceptance.

Install and configure the application

Create a fresh environment and install Gradio together with the loader’s client dependency:

python3 -m venv .venv
. .venv/bin/activate
python -m pip install "gradio==6.31.0" openai

The loader’s versioned source explicitly imports the openai package and provides an installation error if it is absent. Installing Gradio alone therefore does not establish that this optional integration dependency is available.

Provide COMETAPI_KEY and COMETAPI_MODEL to this environment through your local secret configuration. Choose an exact current model identifier that supports CometAPI’s text Chat Completions route and is available to your account. A model name from another provider’s example is not sufficient evidence of compatibility.

Keep the endpoint fixed in the application. The browser should supply a question, while the Python process owns the provider, credential, and selected model. After installation, record the resolved package versions so you can reproduce the environment that passes your checks.

Build the local chat

Save this as app.py:

import os

import gradio as gr


def required_setting(name: str) -> str:
    value = os.environ.get(name, "").strip()
    if not value:
        raise RuntimeError(f"Missing required setting: {name}")
    return value


def build_demo():
    key = required_setting("COMETAPI_KEY")
    model = required_setting("COMETAPI_MODEL")
    return gr.load_chat(
        "https://api.cometapi.com/v1",
        model=model,
        token=key,
        file_types=None,
        streaming=False,
        concurrency_limit=1,
        save_history=False,
        flagging_mode="never",
        title="Local text chat",
        description="Ask a short text question.",
    )


if __name__ == "__main__":
    build_demo().launch(
        server_name="127.0.0.1",
        share=False,
        show_error=False,
    )

Run it with:

python app.py

Open the local URL printed in the terminal. The configuration check reports a missing variable by name without printing its value. Both settings are read before the loader is constructed or the web server is launched.

file_types=None is deliberate. Gradio’s loader otherwise defaults to "text_encoded", which allows uploaded text files to be appended to the prompt. Starting with a text box makes the first connection check easier to inspect and avoids introducing file handling into this example.

streaming=False selects the loader’s non-streaming branch. The answer appears after that call returns. Once ordinary text chat works, you can evaluate streaming separately by changing this one loader argument; CometAPI documents streamed Chat Completions as Server-Sent Events.

Keep interface controls separate from request controls

The loader accepts provider connection arguments and passes additional keyword arguments to ChatInterface. The versioned ChatInterface source documents the interface settings used above.

concurrency_limit=1 limits simultaneously running chatbot submissions for this interface. It does not establish an account spending ceiling, bound the length of a conversation, or coordinate limits across separately running application instances. Those require application policy beyond this local interface setting.

save_history=False leaves Gradio’s optional browser local-storage conversation feature disabled. The active conversation still supplies context for subsequent turns. Keep a separate retention policy for provider records, application logs, and any storage you add later.

flagging_mode="never" hides the interface’s flagging button. Enabling manual flagging is a separate data-collection decision: Gradio documents saving flagged exchanges to its configured directory. Decide who may collect that material and how it will be retained before turning the feature on.

Provider generation options belong at a different boundary. For example, adding max_completion_tokens to this load_chat call is not a documented way to add that field to the upstream request: the extra keywords are forwarded to ChatInterface. If the chosen model needs particular request parameters, define your own chat callback around a reviewed client. The Python HTTPX client guide provides a separate starting point for explicit transport and response checks.

Contract details to verify

BoundarySetting in this exampleWhat to check
Provider baseDocumented /v1 base URLSupply a client base URL, rather than appending /chat/completions yourself.
Credentialtoken=keyThe value comes from the Python process and is never a user-editable interface field.
ModelCOMETAPI_MODELVerify the exact identifier and support for the selected text route.
Inputfile_types=NoneThe initial interface accepts text without an upload control.
Response modestreaming=FalseThe first acceptance check waits for an ordinary returned answer.
Interface stateExplicit history and flagging settingsConfirm the intended persistence and feedback behavior in your installed version.
ListenerLoopback binding and share=FalseOpen the local address and confirm that no public share link is created.

The official Gradio API reference lists load_chat, ChatInterface, and Blocks.launch separately. That separation explains why connection settings go into the loader while listener settings go into .launch().

Happy-path operator workflow

  1. Confirm that the two required variable names are available to the process without displaying the key. Start the application and open its printed local URL.
  2. Verify that the interface has a text input, no file upload control, and no flagging button. Confirm that startup has not created a public sharing link.
  3. Submit one harmless prompt, such as asking for a short definition of idempotency. Wait for a returned text answer before submitting again.
  4. Ask a short follow-up that refers to the previous answer. This checks that the conversation reaches the model with usable context.
  5. Record the package versions, configured model identifier, time, and acceptance verdict. Keep the credential and conversation contents out of routine diagnostic records.

A successful browser exchange establishes the basic path through this installation. It does not establish every optional model feature or a production capacity limit. Add those acceptance cases when the application’s requirements expand.

Error-path operator workflow

First stop the app and remove one required setting in a separate development shell. Starting app.py should fail with the setting’s name before launching the interface. Restore the configuration before continuing.

Next, in a controlled development environment, use a deliberately unavailable model identifier and submit a harmless question. Confirm that the interface reports a failure instead of presenting an accepted answer. Inspect the underlying status privately, then restore the verified model and restart. Avoid copying raw tracebacks, request headers, or upstream bodies into shared troubleshooting records.

Keep show_error=False for ordinary use. This setting avoids enabling Gradio’s detailed browser error display; it does not sanitize terminal output or implement a custom error mapper. The documented launch signature exposes that switch, while your application remains responsible for its logging policy.

Do not turn a failed submission into an automatic resubmission loop. A browser failure or timeout can leave the remote outcome uncertain, and the loader’s interface concurrency setting does not define an upstream retry budget.

Failure modes

SymptomLikely boundaryNext action
Startup asks for the openai packageOptional loader dependencyInstall it in the same virtual environment used to run the app.
Startup reports a missing settingProcess configurationSupply the named variable to that process without printing the credential.
An upload button appearsLoader input configurationConfirm that the running code sets file_types=None.
The provider rejects the requestCredential, model, or request compatibilityCheck the status and current model support before changing the interface.
An extra generation option causes a constructor errorKeyword forwarded to ChatInterfaceMove request construction into a custom chat callback.
Chat works locally but another person cannot open itExplicit loopback listenerChoose a deliberate authenticated deployment before widening access.
Text appears but the application needs a stricter success verdictCompletion acceptanceAdd a callback that validates the response before returning display text.

FAQ

Why use the base URL rather than the full endpoint?

The loader creates a Chat Completions client, which supplies the operation path. CometAPI’s chat reference specifies the SDK base URL shown in the sample. Supplying the full REST operation as that base can produce an incorrectly composed request URL.

Does the loader validate a complete accepted answer?

The inspected Gradio 6.31.0 non-streaming implementation returns the first choice’s message.content. It does not add the stricter completion policy demonstrated in the site’s HTTPX client guide. If your application must reject truncation, refusal, tool-call outcomes, or unexpected response shapes, validate those conditions in your own callback before returning text to Gradio.

Can I add model parameters through extra loader keywords?

The documented extra keywords customize ChatInterface. They are not a general provider request dictionary. Use a callback when you need explicit model parameters, output limits, transport deadlines, or retry controls, and verify the chosen model’s supported options against the CometAPI reference.

Can I share this application with a team?

Gradio supports several sharing and hosting paths, but enabling a share link makes the interface publicly reachable. Its sharing guide documents authentication and explains that built-in password authentication has limits. Choose access control and spending admission before changing this local example’s exposure. Hiding an API documentation link is not an authentication policy.

Sources checked

Five distinct primary public references returned HTTP 200 on November 10, 2026 UTC:

These references support an evergreen integration task. The release source fixes implementation details to the example’s Gradio version; the maintained documentation supplies the current API and sharing context. Local configuration choices and acceptance procedures are recommendations derived from those contracts.

Reader next step

Run the local text interface, verify a short exchange and follow-up, then check the missing-setting and unavailable-model paths. Decide whether the built-in loader meets your completion and request-control requirements before adding features.

For the account and model setup needed for that check, Start with CometAPI , keep the credential in the Python process, and return to the same narrow acceptance procedure.