55 errors to zero. Nearly all of them traced back to two causes rather than 55. THE DECORATOR. @logged wraps ~24 functions across this package and was declared `def decorator(func: Callable):` with no ParamSpec and no return annotation, so it erased the signature of everything it touched. ToolResult.execute() is annotated `-> ToolResult`; through the decorator it came back Any, and mypy reported 33 no-any-return errors spread across the tools and agents. Each looked like a local annotation slip. All of them were one decorator. Typed with ParamSpec/TypeVar; the async branch casts at the await rather than loosening R, because loosening R would put the Any straight back into every caller. THE MISSING TYPE PARAMETER. BaseAgent was not generic, so _create_agent returned a bare Agent — Agent[Any, Any] — and pydantic_ai then typed every run() result as Any. BaseAgent is now Generic[CtxT] bound to AgentContext, _agent is declared on the base instead of reached through hasattr, and the three tool-registration functions take their agent's real context type. tools_streaming.py already did this; the other three had not been updated. Eight `execute` overrides carry a targeted ignore rather than a package-wide disable_error_code. Every tool narrows the base's **kwargs to its own named parameters, which is a real LSP violation — but nothing anywhere is typed as BaseTool, and every call site constructs the concrete tool. The abstract method earns its place by making a tool without execute impossible to instantiate. The reasoning lives in BaseTool.execute's docstring; the per-site suppressions mean an override that IS unsound still gets caught. BaseAgent.run_stream widened to AsyncIterator[str | StreamEvent], which is what callers already receive: task streams structured events, explore and plan stream strings, and the router branches on isinstance with a comment calling the string path legacy. The annotation now says what the code does. AND THE PART THAT MATTERS MORE THAN THE TYPES. Chasing the last error found that the Ollama sanitiser has been broken. It fetched the parent's chat getter with `AsyncOpenAI.chat.fget`, and openai made `chat` a functools.cached_property, whose getter is `.func`. Touching `.chat` raised AttributeError — meaning the content: null workaround that CLAUDE.md documents as live would have failed on the first completion any agent attempted. Confirmed in the running container (openai 2.46.0) as well as locally (2.15.0). Two things hid it. The line carried a bare `# type: ignore`, which suppressed precisely the complaint that would have caught it. And /agents/run and /agents/stream have served zero requests in 30 days, so nothing exercised the path. A mitigation can rot completely while every check stays green, if no check actually runs it. The lookup now reads whichever getter the descriptor exposes and raises a legible TypeError if openai adopts a third shape. tests/test_ollama_provider.py walks the chain an agent request walks, short of the network call — mutation-checked: all four fail against the old lookup. 215 passed, 23 skipped, plus the four new. mypy clean over 90 files. Co-Authored-By: Claude <noreply@anthropic.com>
56 lines
2.5 KiB
Python
56 lines
2.5 KiB
Python
"""The sanitising Ollama client must actually be reachable.
|
|
|
|
src/ollama/provider.py exists to work around Ollama rejecting assistant
|
|
messages that carry `content: null` alongside `tool_calls`. On 2026-08-11 it
|
|
raised AttributeError the moment anything touched `.chat`: it fetched the
|
|
parent's getter via `AsyncOpenAI.chat.fget`, and openai had made `chat` a
|
|
functools.cached_property, whose getter is `.func`.
|
|
|
|
Nothing caught it. The line carried a bare `# type: ignore`, so mypy stayed
|
|
quiet, and the endpoints that reach this code had served no requests in 30 days,
|
|
so no user hit it either. The mitigation was dead and everything looked fine.
|
|
|
|
These tests exercise the path rather than the types, because the failure was a
|
|
runtime attribute lookup that no annotation would have caught.
|
|
"""
|
|
from openai import AsyncOpenAI
|
|
|
|
from src.ollama.provider import _parent_chat, _SanitizedAsyncOpenAI, get_ollama_provider
|
|
|
|
|
|
class TestSanitizedClientIsReachable:
|
|
|
|
def test_chat_can_be_accessed(self):
|
|
"""The regression: this raised AttributeError, not a type error."""
|
|
client = _SanitizedAsyncOpenAI(base_url="http://localhost:11434/v1")
|
|
chat = client.chat
|
|
assert chat is not None
|
|
assert chat.completions is not None
|
|
|
|
def test_provider_reaches_completions(self):
|
|
"""The full chain an agent request walks, short of the network call."""
|
|
provider = get_ollama_provider()
|
|
assert provider._openai_client.chat.completions is not None
|
|
|
|
def test_parent_lookup_survives_either_descriptor_shape(self):
|
|
"""openai has used both property and cached_property for `chat`.
|
|
|
|
Whichever it is, the parent's own getter must be found — the previous
|
|
code hardcoded `.fget` and broke on the switch to cached_property.
|
|
"""
|
|
descriptor = AsyncOpenAI.__dict__["chat"]
|
|
assert hasattr(descriptor, "func") or hasattr(descriptor, "fget"), (
|
|
"AsyncOpenAI.chat exposes neither getter; _parent_chat needs updating"
|
|
)
|
|
client = _SanitizedAsyncOpenAI(base_url="http://localhost:11434/v1")
|
|
assert _parent_chat(client) is not None
|
|
|
|
def test_parent_chat_is_not_the_override(self):
|
|
"""It must return openai's Chat, not recurse into the subclass property.
|
|
|
|
Returning the subclass's own `chat` would be infinite recursion, and the
|
|
sanitiser would wrap itself instead of the real completions resource.
|
|
"""
|
|
client = _SanitizedAsyncOpenAI(base_url="http://localhost:11434/v1")
|
|
assert type(_parent_chat(client)).__name__ != "_SanitizedChat"
|