fix: support Haystack 3.0's lazy OpenAI client initialization in OpenAI-inheriting generators#3536
Merged
Merged
Conversation
Haystack 3.0 no longer creates the OpenAI clients in __init__: they are None until warm_up()/warm_up_async(), and a missing API key raises at warm-up instead of init. Adapt the OpenAI-inheriting integrations while keeping 2.x behavior: - mistral, openrouter: replace the removed _is_warmed_up flag with an unconditional idempotent warm_up() (warm_up_async in run_async when available) and narrow the now-Optional clients for mypy. - perplexity: only wrap the clients with attribution headers in __init__ when they exist (2.x); override warm_up/warm_up_async to inject the headers when 3.0 creates the clients. - tests: warm the component up before asserting on client attributes and accept the API-key error from either __init__ (2.x) or warm_up (3.0). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Contributor
Contributor
Contributor
Coverage report (openrouter)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Contributor
Contributor
Contributor
Contributor
Coverage report (mistral)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
…dder perplexity's ruff config forbids assert outside tests (S101); raise a RuntimeError instead when the client is missing, which narrows the Optional type for mypy just as well. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
Coverage report (perplexity)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
sjrl
reviewed
Jul 3, 2026
sjrl
reviewed
Jul 3, 2026
sjrl
reviewed
Jul 3, 2026
sjrl
reviewed
Jul 3, 2026
Review feedback on #3536: drop the warm-up dev comments in run(), and replace the assert / raise-RuntimeError narrowing with type: ignore[union-attr] comments, consistent with how the repo usually silences mypy for attributes that are Optional only with haystack-ai >= 3.0. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
anakin87
requested changes
Jul 6, 2026
anakin87
left a comment
Member
There was a problem hiding this comment.
I've left some general considerations on tests.
(I would probably have created separate PRs for each integration where we modify the source code.) Anyway, the important thing is releasing the integrations once merged to make them v3-ready)
…undant from_dict env-var tests - test_init_default/test_init_with_parameters no longer call warm_up(); they assert on api_key.resolve_value() instead of client attributes - client creation is covered by a new dedicated test_warm_up - test_from_dict_fail_wo_env_var removed (redundant with test_init_fail_wo_api_key) - test_init_default_async renamed to test_warm_up_async (it tests client creation) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…client_kwargs from an internal attribute Per review suggestion: keep the attribution-enriched http_client_kwargs on self.http_client_kwargs so haystack-ai >= 3.0 builds the clients with the header at warm-up, and store the original user-provided value under self._http_client_kwargs for to_dict(). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Related Issues
Haystack 3.0 no longer creates the OpenAI clients in
__init__:self.client/self.async_clientareNoneuntilwarm_up()/warm_up_async(), the_is_warmed_upflag is gone, and a missing API key raises at warm-up instead of at init. This breaks the ten integrations that subclassOpenAIChatGenerator/OpenAIGenerator/OpenAIResponsesChatGenerator/the OpenAI embedders.Proposed Changes:
Source changes
run/run_asyncoverrides): replace the removedif not self._is_warmed_up: self.warm_up()with an unconditionalself.warm_up()(idempotent on both versions); inrun_async, callwarm_up_async()when it exists (3.0) so the async client is created on the running event loop; narrow the now-Optionalclients for mypy with the sameassert-after-warm-up idiom that haystack 3.0's ownrun()uses.__init__when they exist (2.x); addwarm_up/warm_up_asyncoverrides that apply the headers when 3.0 creates the clients.http_client_kwargs, but serialization must keep the user-provided value — under 3.0 the client is built fromself.http_client_kwargsat warm-up, losing the header. Keep the enriched kwargs onself.http_client_kwargs(so both 2.x init and 3.0 warm-up build the clients with the header) and store the original user-provided value asself._http_client_kwargs, whichto_dict()serializes.Test changes (aimlapi, cometapi, meta_llama, mistral, nvidia, openrouter, orcarouter, perplexity, stackit, togetherai)
test_init_default/test_init_with_parameters: no longer assert on client attributes; they checkcomponent.api_key.resolve_value()and the other init attributes. Client creation is covered by a new dedicatedtest_warm_up(callswarm_up(), harmless on 2.x, required on 3.0).test_init_fail_wo_api_key: runs init andwarm_up()insidepytest.raises(ValueError), accepting the error from either version's raise point. The error message is identical on both.test_from_dict_fail_wo_env_varis removed as redundant with this test.test_init_default_async→test_warm_up_async: async test thatawait component.warm_up_async()when available, since 3.0 only creates the async client there.How did you test it?
git+https://github.com/deepset-ai/haystack.git@v3into the test envs; for the seven suites that also need theToolInvokerimport guard I verified on a local merge with test: guard ToolInvoker imports so chat-generator tests run under Haystack 3.0 #3535):test:typesis clean in all ten — the_is_warmed_up/OpenAI | Nonemypy errors from the sweep are gone.Notes for the reviewer
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.🤖 Generated with Claude Code