oc_chat
Loading...
Searching...
No Matches
providers.ollama.client Namespace Reference

Classes

class  OllamaClient

Functions

http.client.HTTPConnection _connect (str base_url, int timeout=300)
None _raise_for_status (http.client.HTTPResponse resp, str base_url, str method, str path)
http.client.HTTPResponse _post (str base_url, str path, dict body, int timeout=300)
http.client.HTTPResponse _get (str base_url, str path, int timeout=30)

Variables

int _ERROR_BODY_TRUNCATE = 2048

Detailed Description

Ollama HTTP client — native /api/chat streaming.

Purpose
-------
:class:`OllamaClient` implements :class:`BaseClient` against Ollama's
native ``/api/chat`` endpoint (not the OpenAI-compat shim). Streaming is
parsed via :func:`providers.ollama.ndjson.parse_ndjson_stream`.

Responsibilities
----------------
- :class:`OllamaClient` — :class:`BaseClient` for the Ollama provider.
  ``stream_chat``, ``one_shot``, ``list_models``, ``show_model``.
- :func:`_connect` — build an ``HTTPConnection`` / ``HTTPSConnection``
  from the base URL (stdlib only, zero extra deps).
- Friendly ``RuntimeError`` when the Ollama daemon is not running.

Diagnostics
-----------
Domain: OC_CHAT.CLIENT
Levels:
    L2 — lifecycle events (connect, response status)
    L3 — request details (model, endpoint, chunk counts)
    L4 — deep tracing

Contracts
---------
- ``stream_chat`` yields ``("content", str)``, ``("tool_call", dict)``,
  and ``("usage", dict)`` tuples. ``"reasoning"`` is never yielded (local
  models don't separate reasoning from content in the Ollama protocol).
- ``tool_call`` dict: ``{"id": str, "name": str, "arguments": str}``
  where *arguments* is a JSON string (matches the rest of OC Chat).
- ``one_shot`` returns a plain ``str``; uses ``stream: false``.
- ``list_models`` hits ``GET /api/tags``; ``show_model`` hits
  ``POST /api/show``.
- Connection errors raise ``RuntimeError`` with a user-friendly message
  suggesting ``ollama serve``.

Dependencies
------------
- oc_diagnostics (_dbg)
- providers.ollama.ndjson (parse_ndjson_stream)
- http.client (HTTP and HTTPS connections to the Ollama daemon)
- json (encoding request bodies and tool-call arguments, decoding responses)
- urllib.parse (splitting the base URL into scheme, host and port)
- oc_chat.core.egress_guard (lazy; refusing a forbidden internal address as the Ollama host)
- option_c_agent_contracts (lazy; SupportsCancellation type of the stream_chat cancellation token)

Function Documentation

◆ _connect()

http.client.HTTPConnection providers.ollama.client._connect ( str base_url,
int timeout = 300 )
protected
Return an ``HTTPConnection`` or ``HTTPSConnection`` for *base_url*.

Definition at line 71 of file client.py.

Here is the caller graph for this function:

◆ _get()

http.client.HTTPResponse providers.ollama.client._get ( str base_url,
str path,
int timeout = 30 )
protected
GET *base_url + path*. Returns the response.

Raises identically to :func:`_post` — see its docstring for the HTTP-error
semantics (plan 077). The same swallow-on-error bug applied here for
``/api/tags`` / ``/api/show`` callers.

Definition at line 159 of file client.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _post()

http.client.HTTPResponse providers.ollama.client._post ( str base_url,
str path,
dict body,
int timeout = 300 )
protected
POST *body* as JSON to *base_url + path*. Returns the response.

Raises :class:`RuntimeError` on connection failure (unreachable daemon)
and on HTTP 4xx/5xx responses — the latter via :func:`_raise_for_status`,
which embeds the daemon's response body in the exception message
(plan 077; previously the error body was silently swallowed).

Definition at line 130 of file client.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _raise_for_status()

None providers.ollama.client._raise_for_status ( http.client.HTTPResponse resp,
str base_url,
str method,
str path )
protected
Raise :class:`RuntimeError` when *resp* is a 4xx/5xx HTTP response.

Plan 077: the Ollama daemon returns clear, useful error bodies on 4xx/5xx
(e.g. ``{"error":"model 'X' not found"}``). The previous implementation
handed the response straight to the NDJSON parser, which silently yielded
zero chunks for an error body, masquerading as "no response content
received". This helper surfaces the daemon's actual message instead.

Reads the body eagerly (truncated to :data:`_ERROR_BODY_TRUNCATE`) and
raises so callers never see a partially-consumed response on the error
path. Callers consuming success responses (200-299) get the response
untouched.

Definition at line 94 of file client.py.

Here is the caller graph for this function:

Variable Documentation

◆ _ERROR_BODY_TRUNCATE

int providers.ollama.client._ERROR_BODY_TRUNCATE = 2048
protected

Definition at line 91 of file client.py.