Repository navigation
Add Ollama model readiness check to PydanticAIHook - #75369
Merged
Merged
Conversation
Lee-W
force-pushed
the
common-ai-local-model-readiness
branch
4 times, most recently
from
October 30, 2026 08:21
ff80256 to
5be972c
Compare
Lee-W
marked this pull request as ready for review
October 30, 2026 08:23
Member
Author
|
This is a use case from a COC glasgow speaker |
Lee-W
force-pushed
the
common-ai-local-model-readiness
branch
2 times, most recently
from
November 2, 2026 04:48
5e9c3a7 to
9a2ee40
Compare
uranusjr
reviewed
Nov 2, 2026
uranusjr
reviewed
Nov 2, 2026
uranusjr
reviewed
Nov 2, 2026
uranusjr
reviewed
Nov 2, 2026
kaxil
reviewed
Nov 3, 2026
Lee-W
force-pushed
the
common-ai-local-model-readiness
branch
from
November 3, 2026 14:42
3e875ea to
f2c147a
Compare
uranusjr
reviewed
Nov 4, 2026
uranusjr
reviewed
Nov 4, 2026
uranusjr
reviewed
Nov 4, 2026
uranusjr
reviewed
Nov 4, 2026
uranusjr
reviewed
Nov 4, 2026
uranusjr
reviewed
Nov 4, 2026
kaxil
reviewed
Nov 4, 2026
kaxil
approved these changes
Nov 4, 2026
- 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().
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
force-pushed
the
common-ai-local-model-readiness
branch
from
November 9, 2026 07:35
f2c147a to
571405d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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./api/ps. If the connection's model is not loaded, it sends a load request to/api/generateand polls/api/psuntil the model shows up. Tasks call it beforecreate_agent().NotImplementedError. Only the hook's own connection is checked, notfallback_conn_ids.timeoutis the total budget. Each request is capped by the time left, and a timeout surfaces asTimeoutErrornaming the model.timeoutis used up. Other HTTP errors are raised as is.timeout/poll_interval, a missing model and a missing connection host raiseValueError.hooks/ollama.py(OllamaBackend), registered inprovider.yamlandget_provider_info.py.httpx2>=2.7is now a declaredcommon.aidepenREADME.rst,docs/index.rst,uv.lock`), since thebackend calls it directly.self_hosted_models.rstdocuments the method and its limits.Not done on purpose:
time.sleepand holds the worker slot for the whole wait. (can add an async version after this)Was generative AI tooling used to co-author this PR?
Generated-by: [Claude] following the guidelines
{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.