Ollama Python Library

repository·main·Indexed 27 days ago

https://github.com/ollama/ollama-python

A high-level interface for integrating Python 3.8+ projects with the Ollama API. It supports local and cloud-based model execution, providing synchronous and asynchronous clients (AsyncClient) for tasks such as chatting, generating responses, creating embeddings, and managing models via methods like chat, generate, pull, and push.

Tokens
1.7K
Snippets
8
Records
9
Agent score
45%

What's inside ollama-python

  1. Access Cloud Models via Ollama Cloud API

    main

    Access models directly at https://ollama.com by configuring a Client with the cloud host and an API key.

    1. Create an API key at ollama.com/settings/keys.
    2. Set the OLLAMA_API_KEY environment variable.
    3. Initialize the Client with host='https://ollama.com' and the appropriate Authorization header.
    import os
    from ollama import Client
    
    client = Client(
        host='https://ollama.com',
        headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')}
    )
    
    messages = [
      {
        'role': 'user',
        'content': 'Why is the sky blue?',
      },
    ]
    
    for part in client.chat('gpt-oss:120b', messages=messages, stream=True):
      print(part.message.content, end='', flush=True)
  2. Run Cloud Models via local Ollama

    main

    You can run large cloud models through your local Ollama installation.

    1. Sign in locally: ollama signin
    2. Pull a cloud model: ollama pull <model_name> (e.g., ollama pull gpt-oss:120b-cloud)
    3. Use the Client to interact with it.
    from ollama import Client
    
    client = Client()
    
    messages = [
      {
        'role': 'user',
        'content': 'Why is the sky blue?',
      },
    ]
    
    for part in client.chat('gpt-oss:120b-cloud', messages=messages, stream=True):
      print(part.message.content, end='', flush=True)
  3. Handle Ollama ResponseErrors

    main

    Errors are raised if requests return an error status or if an error occurs during streaming. Use ollama.ResponseError to catch these exceptions and inspect the error message and status_code.

    model = 'does-not-yet-exist'
    
    try:
      ollama.chat(model)
    except ollama.ResponseError as e:
      print('Error:', e.error)
      if e.status_code == 404:
        ollama.pull(model)
  4. Stream chat responses

    main

    To receive responses in chunks, set stream=True in the chat function. This returns a generator that you can iterate over.

    from ollama import chat
    
    stream = chat(
        model='gemma3',
        messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
        stream=True,
    )
    
    for chunk in stream:
      print(chunk['message']['content'], end='', flush=True)
  5. Use the chat API for basic interactions

    main

    Use the chat function to send messages to a model. The response can be accessed either as a dictionary or by accessing attributes directly on the ChatResponse object.

    from ollama import chat
    from ollama import ChatResponse
    
    response: ChatResponse = chat(model='gemma3', messages=[
      {
        'role': 'user',
        'content': 'Why is the sky blue?',
      },
    ])
    print(response['message']['content'])
    # or access fields directly from the response object
    print(response.message.content)
  6. Create a custom Client

    main

    Instantiate the Client class to specify a custom host or headers. All extra keyword arguments are passed to the underlying httpx.Client.

    from ollama import Client
    client = Client(
      host='http://localhost:11434',
      headers={'x-some-header': 'some-value'}
    )
    response = client.chat(model='gemma3', messages=[
      {
        'role': 'user',
        'content': 'Why is the sky blue?',
      },
    ])
  7. Use the AsyncClient for asynchronous requests

    main

    The AsyncClient allows for non-blocking requests. When stream=True is used, the function returns an asynchronous generator.

    import asyncio
    from ollama import AsyncClient
    
    async def chat():
      message = {'role': 'user', 'content': 'Why is the sky blue?'}
      response = await AsyncClient().chat(model='gemma3', messages=[message])
    
    asyncio.run(chat())
    import asyncio
    from ollama import AsyncClient
    
    async def chat():
      message = {'role': 'user', 'content': 'Why is the sky blue?'}
      async for part in await AsyncClient().chat(model='gemma3', messages=[message], stream=True):
        print(part['message']['content'], end='', flush=True)
    
    asyncio.run(chat())
  8. Reference: Ollama Python API methods

    main

    The library provides a high-level API mirroring the Ollama REST API. Common methods include:

    • chat(model, messages, ...): Chat with a model.
    • generate(model, prompt, ...): Generate a response from a prompt.
    • list(): List models.
    • show(model): Show model information.
    • create(model, from_, system, ...): Create a model.
    • copy(source, destination): Copy a model.
    • delete(model): Delete a model.
    • pull(model): Pull a model.
    • push(model): Push a model.
    • embed(model, input, ...): Generate embeddings (supports single string or list of strings).
    • ps(): List running models.