{"data":{"slug":"ollama-ollama-python","name":"ollama-python","tagline":"Ollama Python library","github_url":"https://github.com/ollama/ollama-python","owner":"ollama","repo":"ollama-python","owner_avatar_url":"https://avatars.githubusercontent.com/u/151674099?v=4","primary_language":"Python","stars":10290,"forks":1110,"topics":["ollama","python"],"archived":false,"github_pushed_at":"2026-07-15T03:49:42+00:00","maintenance_label":"Very active","url":"https://www.graphcanon.com/tools/ollama-ollama-python","markdown_url":"https://www.graphcanon.com/tools/ollama-ollama-python.md","api_url":"https://www.graphcanon.com/api/graphcanon/tools/ollama-ollama-python","graph_url":"https://www.graphcanon.com/api/graphcanon/graph?tool=ollama-ollama-python","description":"Ollama Python library","homepage_url":"https://ollama.com","license":"MIT","open_issues":180,"watchers":78,"ai_summary":null,"readme_excerpt":"# Ollama Python Library\n\nThe Ollama Python library provides the easiest way to integrate Python 3.8+ projects with [Ollama](https://github.com/ollama/ollama).\n\n## Prerequisites\n\n- [Ollama](https://ollama.com/download) should be installed and running\n- Pull a model to use with the library: `ollama pull <model>` e.g. `ollama pull gemma3`\n  - See [Ollama.com](https://ollama.com/search) for more information on the models available.\n\n## Install\n\n```sh\npip install ollama\n```\n\n## Usage\n\n```python\nfrom ollama import chat\nfrom ollama import ChatResponse\n\nresponse: ChatResponse = chat(model='gemma3', messages=[\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n])\nprint(response['message']['content'])\n# or access fields directly from the response object\nprint(response.message.content)\n```\n\nSee [_types.py](ollama/_types.py) for more information on the response types.\n\n## Streaming responses\n\nResponse streaming can be enabled by setting `stream=True`.\n\n```python\nfrom ollama import chat\n\nstream = chat(\n    model='gemma3',\n    messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],\n    stream=True,\n)\n\nfor chunk in stream:\n  print(chunk['message']['content'], end='', flush=True)\n```\n\n## Cloud Models\n\nRun larger models by offloading to Ollama’s cloud while keeping your local workflow.\n\n- Supported models: `deepseek-v3.1:671b-cloud`, `gpt-oss:20b-cloud`, `gpt-oss:120b-cloud`, `kimi-k2:1t-cloud`, `qwen3-coder:480b-cloud`, `kimi-k2-thinking` See [Ollama Models - Cloud](https://ollama.com/search?c=cloud) for more information\n\n### Run via local Ollama\n\n1) Sign in (one-time):\n\n```\nollama signin\n```\n\n2) Pull a cloud model:\n\n```\nollama pull gpt-oss:120b-cloud\n```\n\n3) Make a request:\n\n```python\nfrom ollama import Client\n\nclient = Client()\n\nmessages = [\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n]\n\nfor part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):\n  print(part.message.content, end='', flush=True)\n```\n\n### Cloud API (ollama.com)\n\nAccess cloud models directly by pointing the client at `https://ollama.com`.\n\n1) Create an API key from [ollama.com](https://ollama.com/settings/keys) , then set:\n\n```\nexport OLLAMA_API_KEY=your_api_key\n```\n\n2) (Optional) List models available via the API:\n\n```\ncurl https://ollama.com/api/tags\n```\n\n3) Generate a response via the cloud API:\n\n```python\nimport os\nfrom ollama import Client\n\nclient = Client(\n    host='https://ollama.com',\n    headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')}\n)\n\nmessages = [\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n]\n\nfor part in client.chat('gpt-oss:120b', messages=messages, stream=True):\n  print(part.message.content, end='', flush=True)\n```\n\n## Custom client\nA custom client can be created by instantiating `Client` or `AsyncClient` from `ollama`.\n\nAll extra keyword arguments are passed into the [`httpx.Client`](https://www.python-httpx.org/api/#client).\n\n```python\nfrom ollama import Client\nclient = Client(\n  host='http://localhost:11434',\n  headers={'x-some-header': 'some-value'}\n)\nresponse = client.chat(model='gemma3', messages=[\n  {\n    'role': 'user',\n    'content': 'Why is the sky blue?',\n  },\n])\n```\n\n## Async client\n\nThe `AsyncClient` class is used to make asynchronous requests. It can be configured with the same fields as the `Client` class.\n\n```python\nimport asyncio\nfrom ollama import AsyncClient\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  response = await AsyncClient().chat(model='gemma3', messages=[message])\n\nasyncio.run(chat())\n```\n\nSetting `stream=True` modifies functions to return a Python asynchronous generator:\n\n```python\nimport asyncio\nfrom ollama import AsyncClient\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  async for part in await AsyncClient().chat(model='gemma3', messages=[message], stream=True):\n    print(part['message']['content'], end='', flush=True)\n\nasyncio.run(chat())","github_created_at":"2023-12-09T09:27:18+00:00","created_at":"2026-07-15T11:03:07.922471+00:00","updated_at":"2026-07-15T11:03:11.214157+00:00","categories":[{"slug":"developer-tools","name":"Developer Tools","url":"https://www.graphcanon.com/categories/developer-tools","markdown_url":"https://www.graphcanon.com/categories/developer-tools.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/developer-tools"},{"slug":"inference-serving","name":"Inference & Serving","url":"https://www.graphcanon.com/categories/inference-serving","markdown_url":"https://www.graphcanon.com/categories/inference-serving.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/inference-serving"}],"tags":[{"slug":"ollama","name":"ollama"},{"slug":"python","name":"python"}],"trust":{"provenance":{"is_fork":false,"github_id":729453988,"owner_type":"Organization","methodology":"github_public_v1","parent_repo":null,"near_duplicate_slugs":[]},"computed_at":"2026-07-15T11:03:08.987Z","maintenance":{"label":"Very active","score":96,"methodology":"github_public_v1","releases_90d":1,"days_since_push":0,"last_release_at":"2026-04-29T21:20:52Z"},"security_summary":{"status":"findings","scanner":"osv@v1","low_count":4,"high_count":0,"last_scan_at":"2026-07-15T11:03:09.484Z","medium_count":0,"scan_profile":"deps","critical_count":0}},"capability_facts":{"scan":{"source":"repo_scan","observed_at":"2026-07-15T11:03:08.738Z"},"languages":{"value":["python"],"source":"github.language+pyproject.toml","observed_at":"2026-07-15T11:03:08.738Z"},"license_spdx":{"value":"MIT","source":"github.license","observed_at":"2026-07-15T11:03:08.738Z"}}}}