fix: support Haystack 3.0's lazy OpenAI client initialization in OpenAI-inheriting generators#3536
Open
julian-risch wants to merge 3 commits into
Open
fix: support Haystack 3.0's lazy OpenAI client initialization in OpenAI-inheriting generators#3536julian-risch wants to merge 3 commits into
julian-risch wants to merge 3 commits into
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
| max_retries=max_retries, | ||
| http_client_kwargs=_http_client_kwargs_with_attribution(http_client_kwargs), | ||
| ) | ||
| self.http_client_kwargs = http_client_kwargs |
Contributor
There was a problem hiding this comment.
I wonder if instead of the new warm_up methods we should instead store the original user provided http_client_kwargs under a new key like self._http_client_kwargs = http_client_kwargs and then update the to_dict method to use this internal key.
Then I would set self.http_client_kwargs = _http_client_kwargs_with_attribution(http_client_kwargs) which would avoid the need for the new warm_up methods. WDYT?
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>
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 the serializedself.http_client_kwargsintentionally keeps the user-provided value — under 3.0 the client is built from that attribute at warm-up, losing the header. Addwarm_up/warm_up_asyncoverrides that swap the enriched kwargs in during client creation (no-ops on 2.x, which has no embedder warm-up and builds the client in__init__).Test changes (aimlapi, cometapi, meta_llama, mistral, nvidia, openrouter, orcarouter, perplexity, stackit, togetherai)
test_init_default/test_init_with_parameters: callcomponent.warm_up()before asserting on client attributes (harmless on 2.x, required on 3.0).test_init_fail_wo_api_key/test_from_dict_fail_wo_env_var: run init/from_dictandwarm_up()insidepytest.raises(ValueError), accepting the error from either version's raise point. The error message is identical on both.test_init_default_async: converted to async tests 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