Skip to content

fix(providers/ollama): return LLMResponse from generate_with_usage, matching all other providers#193

Merged
himanshu231204 merged 1 commit into
OpenAgentHQ:mainfrom
Nitjsefnie:fix/78-ollama-llmresponse
Jul 17, 2026
Merged

fix(providers/ollama): return LLMResponse from generate_with_usage, matching all other providers#193
himanshu231204 merged 1 commit into
OpenAgentHQ:mainfrom
Nitjsefnie:fix/78-ollama-llmresponse

Conversation

@Nitjsefnie

Copy link
Copy Markdown
Contributor

Fixes #78.

Both generate_with_usage() (sync) and generate_with_usage_async() in the Ollama provider now return LLMResponse — content, model, usage, provider, latency_ms (monotonic-clocked around the HTTP call) — mirroring the exact construction pattern in the OpenAI and Groq providers. Docstrings updated; the sync one's stale "use generate() directly" note now points at the async variant.

Call-site inventory (why nothing else changed)

  • core/pipeline.py:196 already reads .content/.usage/.latency_ms — it assumed LLMResponse all along; this fix makes Ollama conform.
  • No tuple-unpack (text, usage = ...) consumer of these methods exists anywhere in src, tests, or examples (grepped).
  • The base class declares only generate/get_token_count, so no ABC signature to realign.

Tests

5 new mocked-HTTP tests (TestOllamaGenerateWithUsage): full field assertions for sync + async, model-override for both, and async connection-error behavior. Mutation-verified: with ollama.py reverted to the tuple version, exactly the 4 shape tests fail (isinstance(result, LLMResponse)), the error-behavior test rightly still passes.

Verification

tests/unit: 904 passed / 4 skipped, plus one pre-existing failure (TestBERTScore::test_identical) identical on clean main. Ollama's file: 19 passed. ruff check + format clean on touched files (formatting normalized a few pre-existing long lines in the two files touched); mypy (3.11): same 164 pre-existing errors as main, zero in ollama.py, none added. tests/integration crashes identically on clean main in this environment (torch/numpy) and contains no ollama references.

One thing this PR deliberately does NOT do

Ollama's methods keep their current names. Note that pipeline.py awaits generate_with_usage — which is async on every other provider but sync on Ollama — so the pipeline + Ollama combination was and remains broken in a separate way (the awaited non-awaitable raises TypeError, which the bare except Exception swallows into an empty answer). Renaming is an API decision, so I'm filing it as its own issue rather than smuggling it in here.


Generated by Claude Fable 5 (brief, review), Claude Opus 4.8 (1M context) (implementation)

…atching all other providers (fixes OpenAgentHQ#78)

Both Ollama.generate_with_usage (sync) and generate_with_usage_async
returned tuple[str, TokenUsage] while every other LLM provider (OpenAI,
Gemini, Groq, Mock, OpenRouter) returns an LLMResponse. This broke
polymorphic use (e.g. the pipeline reads response.content/.usage/
.latency_ms) and dropped provider/latency metadata.

Both methods now build and return an LLMResponse populated like the
sibling providers: content, model, usage, provider, and a latency_ms
measured with time.monotonic() around the HTTP call.

Tests: add sync + async LLMResponse-contract tests (type and every
field) plus model-override and connection-error coverage, all with a
mocked HTTP client (no live ollama server).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@himanshu231204
himanshu231204 merged commit 513fbbb into OpenAgentHQ:main Jul 17, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown

🎉 Congratulations @Nitjsefnie!

Your pull request has been successfully merged into main. 🚀

Thank you for contributing to OpenAgentHQ and helping improve the project.

We truly appreciate your contribution and hope to see you back with more amazing PRs!

Happy Open Sourcing! ❤️

@himanshu231204

Copy link
Copy Markdown
Member

Hi @Nitjsefnie ,

Thank you for your contribution to OpenAgentHQ. We truly appreciate your time and effort in helping improve the project.

We've recently launched a new open-source project, ModelDock—a package manager for local AI models. We'd love to have you contribute and help shape it as well.

🚀 https://github.com/OpenAgentHQ/modeldock

Looking forward to your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

providers(ollama): generate_with_usage returns tuple[str, TokenUsage] instead of LLMResponse (inconsistent with all other providers)

2 participants