Skip to content

Add Ollama model readiness check to PydanticAIHook - #75369

Merged
Lee-W merged 11 commits into
apache:mainfrom
astronomer:common-ai-local-model-readiness
Nov 10, 2026
Merged

Lee-W merged 11 commits into
apache:mainfrom
astronomer:common-ai-local-model-readiness

Conversation

@Lee-W

@Lee-W Lee-W commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Why

Ollama loads a model into memory on the first request, which can take long enough to time out that request. Tasks using a self-hosted Ollama model currently have no way to wait for the load before building their agent.

What

Adds PydanticAIHook.ensure_model_ready(timeout=300.0, poll_interval=2.0). It is opt-in: nothing calls it automatically.

  • It checks /api/ps. If the connection's model is not loaded, it sends a load request to /api/generate and polls /api/ps until the model shows up. Tasks call it before create_agent().
  • Currnetly only Ollama is supported; any other model prefix raises NotImplementedError. Only the hook's own connection is checked, not fallback_conn_ids.
  • timeout is the total budget. Each request is capped by the time left, and a timeout surfaces as TimeoutError naming the model.
  • The first readiness check and the load request fail fast, so a wrong host or a server error is raised right away. After the load request, connection failures, 5xx responses and single-request timeouts are logged and retried until timeout is used up. Other HTTP errors are raised as is.
  • Non-positive or non-finite timeout / poll_interval, a missing model and a missing connection host raise ValueError.
  • The Ollama HTTP calls live in a new hooks/ollama.py (OllamaBackend), registered in provider.yaml and get_provider_info.py.
  • httpx2>=2.7 is now a declared common.ai depenREADME.rst, docs/index.rst, uv.lock`), since thebackend calls it directly.
  • self_hosted_models.rst documents the method and its limits.

Not done on purpose:

  • The connection password is not sent, so an Ollama server behind authentication is not supported.
  • Polling uses time.sleep and holds the worker slot for the whole wait. (can add an async version after this)
hook = PydanticAIHook(llm_conn_id="pydanticai_defau
hook.ensure_model_ready(timeout=300, poll_interval=2)
agent = hook.create_agent(instructions="Answer briefly.")

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [Claude] following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@Lee-W
Lee-W force-pushed the common-ai-local-model-readiness branch 4 times, most recently from ff80256 to 5be972c Compare October 30, 2026 08:21
@Lee-W
Lee-W marked this pull request as ready for review October 30, 2026 08:23
@Lee-W

Lee-W commented Oct 30, 2026 •

Copy link
Copy Markdown
Member Author

This is a use case from a COC glasgow speaker

@Lee-W
Lee-W force-pushed the common-ai-local-model-readiness branch 2 times, most recently from 5e9c3a7 to 9a2ee40 Compare November 2, 2026 04:48
@Lee-W
Lee-W force-pushed the common-ai-local-model-readiness branch from 3e875ea to f2c147a Compare November 3, 2026 14:42
Lee-W added 3 commits November 9, 2026 15:35
- Add PydanticAIHook.ensure_model_ready(timeout, poll_interval), an opt-in method that
  waits for the primary connection's Ollama model to be loaded before a task builds its
  agent. Other model prefixes raise NotImplementedError.
- Add LocalModelBackend protocol and OllamaBackend (/api/ps, /api/generate) in
  hooks/local_model_backends.py.
- Every request is bounded by the remaining timeout and timeouts surface as
  TimeoutError; after the load request, connection failures and 5xx responses are
  retried until the timeout is used up.
- Declare httpx2 as a common.ai dependency.
- Document the method and its limitations in self_hosted_models.rst.
OllamaBackend.load() already blocks until the model is loaded, so polling afterwards has no purpose. Do one readiness check, one blocking load and one confirming check, and drop poll_interval. Correct the keep_alive comment in OllamaBackend.load().
Lee-W added 8 commits November 9, 2026 15:35
Add a ModelReadinessBackend Protocol and a backend= argument so any server with is_ready() and load() works. Ollama stays the built-in default, picked by a resolver.
The /api/ps listing is not a reliable readiness signal while a model is
still loading, so ensure_model_ready now only sends the empty-prompt
/api/generate request and hands the whole timeout to load(). is_ready,
normalize_ollama_model_name and the RuntimeError path are removed.

backend= is replaced by backend_factory=, which the hook calls with the
resolved model_name and the connection's base_url so a custom backend, or
OllamaBackend behind an openai: model, gets the same values the hook used.
The OllamaBackend tests now assert the timeout given to the client and the
full request body.
- The built-in Ollama path takes the connection host, then the
  OLLAMA_BASE_URL environment variable, as pydantic-ai's OllamaProvider
  does, and raises ValueError only when both are empty.
- Cover a model set only in the connection extra, and a bare model name
  with the model_provider extra.
- Explain how @task.llm and AgentOperator users call ensure_model_ready
  from an upstream task, and that the load request sends no keep_alive,
  so Ollama may unload the model after its own keep-alive time.
- Say that a custom backend_factory receives the connection host or None
  and does not read OLLAMA_BASE_URL.
- Build the backend_factory test double with create_autospec so the
  keyword signature is checked.
…n the backend

- OllamaBackend accepts base_url=None and falls back to OLLAMA_BASE_URL,
  raising ValueError when neither is set, so backend_factory=OllamaBackend
  works for connections without a host
- backend_factory is typed as a ModelReadinessBackendFactory protocol with
  keyword-only model_name and base_url
- ensure_model_ready splits the model name once
- test stub for the factory no longer has an empty body
- OllamaBackend.load re-raises HTTPStatusError with the model name and the
  error field Ollama returned, instead of only the status line and URL
- A body that is not JSON, has no error field, or is blank falls back to the
  response text, then to the reason phrase
… repeating it

- ModelReadinessBackend, ModelReadinessBackendFactory and ensure_model_ready
  keep only their API contract and link to the guide with :ref:
- Add anchors for the Ollama waiting and custom backend sections of the guide
- Move the base_url contract for backend factories into the guide
- Say that the warm-up matters when model_settings sets a timeout shorter
  than pydantic-ai's default HTTP read timeout of 600 seconds
- Note that a warm-up task and the LLM task can reach different Ollama
  servers with per-worker Ollama or replicas behind a load balancer
- Limit the OLLAMA_BASE_URL fallback to ollama: models and point openai:
  models at OPENAI_BASE_URL
@Lee-W
Lee-W force-pushed the common-ai-local-model-readiness branch from f2c147a to 571405d Compare November 9, 2026 07:35
@Lee-W
Lee-W merged commit dfe9cc6 into apache:main Nov 10, 2026
146 of 148 checks passed
@Lee-W
Lee-W deleted the common-ai-local-model-readiness branch November 10, 2026 01:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants