{"data":{"slug":"ollama-ollama-python","name":"ollama-python","tagline":"Python library for integrating projects with Ollama.","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":10539,"forks":1175,"topics":["ollama","python"],"archived":false,"github_pushed_at":"2026-09-16T00:24:28+00:00","maintenance_label":"Very active","stars_delta_30d":137,"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":204,"watchers":81,"ai_summary":"The Ollama Python Library facilitates the integration of Python applications with models and services running on Ollama, supporting chat interactions, response streaming, and cloud model usage.","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 gemma4`\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(\n  model='gemma4',\n  messages=[\n    {\n      'role': 'user',\n      'content': 'Why is the sky blue?',\n    },\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='gemma4',\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(host='https://ollama.com', headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')})\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\n\nclient = Client(host='http://localhost:11434', headers={'x-some-header': 'some-value'})\nresponse = client.chat(\n  model='gemma4',\n  messages=[\n    {\n      'role': 'user',\n      'content': 'Why is the sky blue?',\n    },\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\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  response = await AsyncClient().chat(model='gemma4', messages=[message])\n\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\n\nasync def chat():\n  message = {'role': 'user', 'content': 'Why is the sky blue?'}\n  async for part in await AsyncClient().chat(model='gemma4', messages=[message], stream=True):\n    print(part['message']['content'], end='', flush=True)\n\n\nasy","github_created_at":"2023-12-09T09:27:18+00:00","created_at":"2026-07-15T11:03:07.922471+00:00","updated_at":"2026-09-20T05:11:35.318497+00:00","categories":[{"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"},{"slug":"llm-frameworks","name":"LLM Frameworks","url":"https://www.graphcanon.com/categories/llm-frameworks","markdown_url":"https://www.graphcanon.com/categories/llm-frameworks.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/llm-frameworks"}],"tags":[{"slug":"ai-integration","name":"ai-integration"},{"slug":"chat","name":"chat"},{"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-09-20T05:11:32.488Z","maintenance":{"label":"Very active","score":96,"methodology":"github_public_v1","releases_90d":0,"days_since_push":4,"last_release_at":"2026-04-29T21:20:52Z","stars_delta_30d":137,"open_issues_delta_30d":21},"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-09-20T05:11:33.550Z"},"languages":{"value":["python"],"source":"github.language+pyproject.toml","observed_at":"2026-09-20T05:11:33.550Z"},"license_spdx":{"value":"MIT","source":"github.license","observed_at":"2026-09-20T05:11:33.550Z"}},"decision_facts":{"hosting":null,"pricing":null,"requirements":null,"constraints":null,"when_to_use":["When you need to integrate Python applications with models hosted on Ollama","For seamless chat interaction implementation in a project using Ollama","To utilize Ollama's large cloud-based models directly within your application"],"when_not_to_use":["If your setup does not support or require the integration of Python with Ollama","In projects that require direct model hosting without external services like Ollama","If you prefer using competitive libraries that offer more control over local processing"],"source":"enrich:decision_facts","observed_at":"2026-07-17T10:03:07.591Z"},"constraint_facets":null,"decision_summary":[{"label":"Adopt for","value":"Ollama Python Library simplifies integration of Python projects with Ollama for chat interactions, response streaming, and cloud model usage."}]}}