{"slug": "9router-python-python-sdk-for-the-9router-ai-gateway-35-models", "title": "9router-Python – Python SDK for the 9Router AI gateway (35 models)", "summary": "9Router has released 9router-Python, a Python SDK and CLI for its AI gateway that routes requests to 35 models across providers including Gemini, NVIDIA, OpenRouter, Groq, Ollama, Tavily, and Exa. The SDK supports chat, embeddings, image generation, TTS, STT, web search, and web fetch through a single local endpoint with automatic fallback. It requires Python 3.9+ and is installable via pip.", "body_md": "**Python SDK + CLI for the 9Router AI gateway.**\n\nOne tiny library to talk to an OpenAI-compatible gateway that routes to **Gemini, NVIDIA, OpenRouter, Groq, Ollama, Tavily and Exa** — chat, images, TTS, STT, embeddings and web search/fetch — all through a single local endpoint, with automatic fallback between providers.\n\n``` bash\n$ pip install 9router-python\n```\n\n| Capability | SDK method | CLI command |\n|---|---|---|\n| 💬 Chat (with streaming) | `client.chat()` / `chat_stream()` |\n`ninerouter chat` |\n| 🌀 Embeddings | `client.embeddings()` |\n`ninerouter embed` |\n| 🖼️ Image generation | `client.image()` |\n`ninerouter image` |\n| 🔊 Text-to-speech | `client.tts()` |\n`ninerouter tts` |\n| 🎙️ Speech-to-text | `client.stt()` |\n`ninerouter stt` |\n| 🔎 Web search | `client.web_search()` |\n`ninerouter web search` |\n| 📄 Fetch URL → markdown | `client.web_fetch()` |\n`ninerouter web fetch` |\n| ⚙️ Models & health | `client.models()` , `health()` |\n`ninerouter models` , `health` |\n\nEverything runs through one base URL. Configure once, use everywhere.\n\n``` bash\n$ pip install 9router-python\n$ ninerouter chat \"Hello, world!\" --stream\nHello! 👋 How can I help you today?\n```\n\nThat's it. One gateway, one SDK, every capability.\n\n``` python\nfrom ninerouter import NineRouter\n\nwith NineRouter() as client:\n    reply = client.chat(\"Explain a monad in one sentence\")\n    print(reply.text)\n```\n\nStream tokens as they arrive:\n\n```\nfor chunk in client.chat_stream(\"Write a haiku about Python\"):\n    print(chunk.content, end=\"\", flush=True)\nimage = client.image(\"a watercolor of mountains at sunrise\")\nopen(\"out.png\", \"wb\").write(image.content)\naudio = client.tts(\"Olá, mundo!\", voice=\"pt-BR-FernandaNeural\", model=\"gemini/gemini-3.1-flash-tts-preview\")\nopen(\"speech.wav\", \"wb\").write(audio)\ntext = client.stt(file=(\"recording.wav\", open(\"recording.wav\", \"rb\").read()))\nprint(text.text)\nvector = client.embeddings(\"The quick brown fox\")[0]\ncurl_similarity = sum(a * b for a, b in zip(vector, other))\nresults = client.web_search(\"9Router open source\")\nfor r in results.results:\n    print(r.title, \"-\", r.url)\n\npage = client.web_fetch(\"https://example.com\")\nprint(page.content.text)\nninerouter                          # help\nninerouter health                   # gateway status\nninerouter models --kind chat       # list chat models\nninerouter chat \"Hello\" --stream    # chat with streaming\nninerouter embed \"text\"             # embedding vector\nninerouter image \"a red fox\" --out fox.png\nninerouter tts \"Hello\" --voice pt-BR-FernandaNeural\nninerouter stt recording.wav\nninerouter web search \"9Router\"\nninerouter web fetch https://example.com\nninerouter config                   # show effective config\n```\n\nResolution order: **CLI flags > environment variables > defaults.**\n\n| Setting | Env var | Default |\n|---|---|---|\n| Base URL | `NINEROUTER_URL` |\n`http://localhost:20128` |\n| API key | `NINEROUTER_KEY` |\n(optional) |\n\n```\nexport NINEROUTER_URL=\"http://localhost:20128\"\nexport NINEROUTER_KEY=\"sk-...\"     # optional if gateway has auth disabled\n```\n\nThe key is only sent when non-empty, so a gateway running with `requireApiKey=false`\n\nworks out of the box.\n\n**SSE-by-default:** the gateway often streams SSE even when`stream`\n\nisn't set — the client parses both JSON and SSE transparently.**TTS**: some OpenRouter TTS models return`502`\n\nupstream errors; prefer a`gemini/*`\n\nTTS model.**STT**: multipart upload with`gemini/*`\n\nor`groq/whisper-*`\n\nmodels.**Web search/fetch use**(not`provider`\n\n`model`\n\n), e.g.`tavily`\n\n,`exa`\n\n, or the`search-combo`\n\n.**Combos**(`owned_by: \"combo\"`\n\n, like`Code`\n\n) auto-fallback across providers — great defaults for chat.\n\n```\npip install -e .[dev]\npytest                     # mocked unit tests\npytest -m live             # live tests (needs a running gateway)\n```\n\nRequires Python 3.9+.\n\n9Router gives you keyless access to dozens of AI providers with automatic fallback, so your app keeps working even if one provider's quota or free tier runs out. This SDK removes the friction: you get a typed, tested Python client and a friendly CLI without caring about which endpoint each provider uses.\n\nThis SDK is a *thin HTTP client* — it does not hold AI provider keys, does not\nupload your prompts anywhere other than the gateway you point it at, and ships\n**no telemetry**.\n\n**No secrets in the client.** An optional API key is read from the environment (`NINEROUTER_KEY`\n\n) and sent only to your configured gateway.**Nothing leaves your network except to the gateway you configure.** The SDK never sends data to the SDK author or any third party.**The gateway holds the keys.** Provider API keys (Gemini, NVIDIA, Tavily, …) live in*your*9Router gateway on*your*machine/VM — not in this library.**Supply-chain hygiene.** Only two runtime dependencies (`httpx`\n\n,`typer`\n\n), both widely trusted. CI runs unit tests on every push across Python 3.9/3.11/3.13.**No-secrets guardrail.** A test in CI scans the repo for committed credentials and fails the build if anything slips in.\n\nSee [SECURITY.md](/ChristopherDond/9router-python/blob/main/SECURITY.md) for the full policy and how to report a\nvulnerability privately.\n\nMIT © (ChristopherDond)\n\n**SDK Python + CLI para o gateway de IA 9Router.**\n\nUm único pacote para conversar com um gateway compatível com a API da OpenAI que roteia **Gemini, NVIDIA, OpenRouter, Groq, Ollama, Tavily e Exa** — chat, imagens, TTS, STT, embeddings e busca web, com fallback automático entre provedores.\n\n```\npip install 9router-python\nninerouter chat \"Olá, mundo!\" --stream\n```\n\nConsulte a seção **Features** e **Quickstart** acima — a API e o CLI são idênticos. Para configurar, veja **Configuration**.\n\n``` python\nfrom ninerouter import NineRouter\n\nwith NineRouter() as client:\n    print(client.chat(\"Me conta uma piada\"))\n```\n\n[⬆ back to top](#9router-python)", "url": "https://wpnews.pro/news/9router-python-python-sdk-for-the-9router-ai-gateway-35-models", "canonical_source": "https://github.com/ChristopherDond/9router-python", "published_at": "2026-08-04 00:55:20+00:00", "updated_at": "2026-08-04 01:22:31.759322+00:00", "lang": "en", "topics": ["developer-tools", "ai-infrastructure", "ai-tools"], "entities": ["9Router", "Gemini", "NVIDIA", "OpenRouter", "Groq", "Ollama", "Tavily", "Exa"], "alternates": {"html": "https://wpnews.pro/news/9router-python-python-sdk-for-the-9router-ai-gateway-35-models", "markdown": "https://wpnews.pro/news/9router-python-python-sdk-for-the-9router-ai-gateway-35-models.md", "text": "https://wpnews.pro/news/9router-python-python-sdk-for-the-9router-ai-gateway-35-models.txt", "jsonld": "https://wpnews.pro/news/9router-python-python-sdk-for-the-9router-ai-gateway-35-models.jsonld"}}