Repository: deepset-ai/haystack

Issue:
Add enviroment variable for openai timeout, backoff and max retries
**Is your feature request related to a problem? Please describe.**
In version 1, haystack allows to set three environment variables to manage:
* OPENAI_TIMEOUT
* OPENAI_MAX_RETRIES
* OPENAI_BACKOFF

It's useful also for the versione 2.0

Discussion:
CarlosFerLo: > Thanks for the PR!
> 
> Any reason why we don't also add these settings as `__init__` parameter? In other words, we could have
> 
> ```python
>     def __init__(
>         self,
>         ...
>         timeout: float = 30.0,
>         max_retries: int = 5
>     ):
> ```
> 
> then in the method:
> 
> ```python
>         ...
>         self.client = OpenAI(
>             api_key=api_key.resolve_value(),
>             organization=organization,
>             base_url=api_base_url,
>             timeout=float(os.environ.get("OPENAI_TIMEOUT", timeout)),
>             max_retries=int(os.environ.get("OPENAI_MAX_RETRIES", max_retries)),
>         )
> ```

Don't you think it would be better to make the priority order this way:

PARAMETER -> ENV_VAR -> DEFAULT

Where -> Means overwrites.

masci: @CarlosFerLo yes please go ahead as you suggest!
coveralls: ## Pull Request Test Coverage Report for [Build 9102969524](https://coveralls.io/builds/67515826)


### Details

* **0** of **0**   changed or added relevant lines in **0** files are covered.
* **50** unchanged lines in **5** files lost coverage.
* Overall coverage increased (+**0.03%**) to **90.585%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [telemetry/_environment.py](https://coveralls.io/builds/67515826/source?filename=telemetry%2F_environment.py#L101) | 2 | 84.21% |
| [components/generators/openai.py](https://coveralls.io/builds/67515826/source?filename=components%2Fgenerators%2Fopenai.py#L133) | 3 | 96.34% |
| [components/embedders/openai_text_embedder.py](https://coveralls.io/builds/67515826/source?filename=components%2Fembedders%2Fopenai_text_embedder.py#L101) | 10 | 75.61% |
| [components/embedders/openai_document_embedder.py](https://coveralls.io/builds/67515826/source?filename=components%2Fembedders%2Fopenai_document_embedder.py#L116) | 14 | 79.1% |
| [components/generators/chat/openai.py](https://coveralls.io/builds/67515826/source?filename=components%2Fgenerators%2Fchat%2Fopenai.py#L145) | 21 | 78.9% |
<!-- | **Total:** | **50** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/67515826/badge)](https://coveralls.io/builds/67515826) |
| :-- | --: |
| Change from base [Build 9101640205](https://coveralls.io/builds/67513425): |  0.03% |
| Covered Lines: | 6591 |
| Relevant Lines: | 7276 |

---
##### 💛  - [Coveralls](https://coveralls.io)


Patch:
diff --git a/haystack/components/embedders/openai_document_embedder.py b/haystack/components/embedders/openai_document_embedder.py
index 4bc189dd..b129f68e 100644
--- a/haystack/components/embedders/openai_document_embedder.py
+++ b/haystack/components/embedders/openai_document_embedder.py
@@ -2,6 +2,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
+import os
 from typing import Any, Dict, List, Optional, Tuple
 
 from openai import OpenAI
@@ -45,10 +46,15 @@ class OpenAIDocumentEmbedder:
         progress_bar: bool = True,
         meta_fields_to_embed: Optional[List[str]] = None,
         embedding_separator: str = "\n",
+        timeout: Optional[float] = None,
+        max_retries: Optional[int] = None,
     ):
         """
         Create a OpenAIDocumentEmbedder component.
 
+        By setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' you can change the timeout and max_retries parameters in the OpenAI client.
+
+
         :param api_key:
             The OpenAI API key.
         :param model:
@@ -73,6 +79,10 @@ class OpenAIDocumentEmbedder:
             List of meta fields that will be embedded along with the Document text.
         :param embedding_separator:
             Separator used to concatenate the meta fields to the Document text.
+        :param timeout:
+            Timeout for OpenAI Client calls, if not set it is inferred from the `OPENAI_TIMEOUT` environment variable or set to 30.
+        :param max_retries:
+            Maximum retries to stablish contact with OpenAI if it returns an internal error, if not set it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
         """
         self.api_key = api_key
         self.model = model
@@ -86,7 +96,18 @@ class OpenAIDocumentEmbedder:
         self.meta_fields_to_embed = meta_fields_to_embed or []
         self.embedding_separator = embedding_separator
 
-        self.client = OpenAI(api_key=api_key.resolve_value(), organization=organization, base_url=api_base_url)
+        if timeout is None:
+            timeout = float(os.environ.get("OPENAI_TIMEOUT", 30.0))
+        if max_retries is None:
+            max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", 5))
+
+        self.client = OpenAI(
+            api_key=api_key.resolve_value(),
+            organization=organization,
+            base_url=api_base_url,
+            timeout=timeout,
+            max_retries=max_retries,
+        )
 
     def _get_telemetry_data(self) -> Dict[str, Any]:
         """
diff --git a/haystack/components/embedders/openai_text_embedder.py b/haystack/components/embedders/openai_text_embedder.py
index 347d6a18..ad259c01 100644
--- a/haystack/components/embedders/openai_text_embedder.py
+++ b/haystack/components/embedders/openai_text_embedder.py
@@ -2,6 +2,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
+import os
 from typing import Any, Dict, List, Optional
 
 from openai import OpenAI
@@ -9,6 +10,9 @@ from openai import OpenAI
 from haystack import component, default_from_dict, default_to_dict
 from haystack.utils import Secret, deserialize_secrets_inplace
 
+OPENAI_TIMEOUT = float(os.environ.get("OPENAI_TIMEOUT", 30))
+OPENAI_MAX_RETRIES = int(os.environ.get("OPENAI_MAX_RETRIES", 5))
+
 
 @component
 class OpenAITextEmbedder:
@@ -40,10 +44,14 @@ class OpenAITextEmbedder:
         organization: Optional[str] = None,
         prefix: str = "",
         suffix: str = "",
+        timeout: Optional[float] = None,
+        max_retries: Optional[int] = None,
     ):
         """
         Create an OpenAITextEmbedder component.
 
+        By setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' you can change the timeout and max_retries parameters in the OpenAI client.
+
         :param api_key:
             The OpenAI API key.
         :param model:
@@ -60,6 +68,10 @@ class OpenAITextEmbedder:
             A string to add at the beginning of each text.
         :param suffix:
             A string to add at the end of each text.
+        :param timeout:
+            Timeout for OpenAI Client calls, if not set it is inferred from the `OPENAI_TIMEOUT` environment variable or set to 30.
+        :param max_retries:
+            Maximum retries to stablish contact with OpenAI if it returns an internal error, if not set it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
         """
         self.model = model
         self.dimensions = dimensions
@@ -69,7 +81,18 @@ class OpenAITextEmbedder:
         self.suffix = suffix
         self.api_key = api_key
 
-        self.client = OpenAI(api_key=api_key.resolve_value(), organization=organization, base_url=api_base_url)
+        if timeout is None:
+            timeout = float(os.environ.get("OPENAI_TIMEOUT", 30.0))
+        if max_retries is None:
+            max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", 5))
+
+        self.client = OpenAI(
+            api_key=api_key.resolve_value(),
+            organization=organization,
+            base_url=api_base_url,
+            timeout=timeout,
+            max_retries=max_retries,
+        )
 
     def _get_telemetry_data(self) -> Dict[str, Any]:
         """
diff --git a/haystack/components/generators/chat/openai.py b/haystack/components/generators/chat/openai.py
index 8bef38e3..2bb4a6a9 100644
--- a/haystack/components/generators/chat/openai.py
+++ b/haystack/components/generators/chat/openai.py
@@ -4,6 +4,7 @@
 
 import copy
 import json
+import os
 from typing import Any, Callable, Dict, List, Optional, Union
 
 from openai import OpenAI, Stream
@@ -75,6 +76,8 @@ class OpenAIChatGenerator:
         api_base_url: Optional[str] = None,
         organization: Optional[str] = None,
         generation_kwargs: Optional[Dict[str, Any]] = None,
+        timeout: Optional[float] = None,
+        max_retries: Optional[int] = None,
     ):
         """
         Initializes the OpenAIChatGenerator component.
@@ -82,6 +85,8 @@ class OpenAIChatGenerator:
         Creates an instance of OpenAIChatGenerator. Unless specified otherwise in the `model`, this is for OpenAI's
         GPT-3.5 model.
 
+        By setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' you can change the timeout and max_retries parameters in the OpenAI client.
+
         :param api_key: The OpenAI API key.
         :param model: The name of the model to use.
         :param streaming_callback: A callback function that is called when a new token is received from the stream.
@@ -108,6 +113,10 @@ class OpenAIChatGenerator:
                 Bigger values mean the model will be less likely to repeat the same token in the text.
             - `logit_bias`: Add a logit bias to specific tokens. The keys of the dictionary are tokens, and the
                 values are the bias to add to that token.
+        :param timeout:
+            Timeout for OpenAI Client calls, if not set it is inferred from the `OPENAI_TIMEOUT` environment variable or set to 30.
+        :param max_retries:
+            Maximum retries to stablish contact with OpenAI if it returns an internal error, if not set it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
         """
         self.api_key = api_key
         self.model = model
@@ -115,7 +124,19 @@ class OpenAIChatGenerator:
         self.streaming_callback = streaming_callback
         self.api_base_url = api_base_url
         self.organization = organization
-        self.client = OpenAI(api_key=api_key.resolve_value(), organization=organization, base_url=api_base_url)
+
+        if timeout is None:
+            timeout = float(os.environ.get("OPENAI_TIMEOUT", 30.0))
+        if max_retries is None:
+            max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", 5))
+
+        self.client = OpenAI(
+            api_key=api_key.resolve_value(),
+            organization=organization,
+            base_url=api_base_url,
+            timeout=timeout,
+            max_retries=max_retries,
+        )
 
     def _get_telemetry_data(self) -> Dict[str, Any]:
         """
diff --git a/haystack/components/generators/openai.py b/haystack/components/generators/openai.py
index d2a37bb7..fc2a5aae 100644
--- a/haystack/components/generators/openai.py
+++ b/haystack/components/generators/openai.py
@@ -2,6 +2,7 @@
 #
 # SPDX-License-Identifier: Apache-2.0
 
+import os
 from typing import Any, Callable, Dict, List, Optional, Union
 
 from openai import OpenAI, Stream
@@ -60,10 +61,15 @@ class OpenAIGenerator:
         organization: Optional[str] = None,
         system_prompt: Optional[str] = None,
         generation_kwargs: Optional[Dict[str, Any]] = None,
+        timeout: Optional[float] = None,
+        max_retries: Optional[int] = None,
     ):
         """
         Creates an instance of OpenAIGenerator. Unless specified otherwise in the `model`, this is for OpenAI's GPT-3.5 model.
 
+        By setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' you can change the timeout and max_retries parameters in the OpenAI client.
+
+
         :param api_key: The OpenAI API key.
         :param model: The name of the model to use.
         :param streaming_callback: A callback function that is called when a new token is received from the stream.
@@ -92,6 +98,11 @@ class OpenAIGenerator:
                 Bigger values mean the model will be less likely to repeat the same token in the text.
             - `logit_bias`: Add a logit bias to specific tokens. The keys of the dictionary are tokens, and the
                 values are the bias to add to that token.
+        :param timeout:
+            Timeout for OpenAI Client calls, if not set it is inferred from the `OPENAI_TIMEOUT` environment variable or set to 30.
+        :param max_retries:
+            Maximum retries to stablish contact with OpenAI if it returns an internal error, if not set it is inferred from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
+
         """
         self.api_key = api_key
         self.model = model
@@ -101,7 +112,19 @@ class OpenAIGenerator:
 
         self.api_base_url = api_base_url
         self.organization = organization
-        self.client = OpenAI(api_key=api_key.resolve_value(), organization=organization, base_url=api_base_url)
+
+        if timeout is None:
+            timeout = float(os.environ.get("OPENAI_TIMEOUT", 30.0))
+        if max_retries is None:
+            max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", 5))
+
+        self.client = OpenAI(
+            api_key=api_key.resolve_value(),
+            organization=organization,
+            base_url=api_base_url,
+            timeout=timeout,
+            max_retries=max_retries,
+        )
 
     def _get_telemetry_data(self) -> Dict[str, Any]:
         """
diff --git a/haystack/telemetry/_environment.py b/haystack/telemetry/_environment.py
index 2da0aa35..f5c92b9e 100644
--- a/haystack/telemetry/_environment.py
+++ b/haystack/telemetry/_environment.py
@@ -12,7 +12,6 @@ from haystack.version import __version__
 
 logger = logging.getLogger(__name__)
 
-
 # This value cannot change during the lifetime of the process
 _IS_DOCKER_CACHE = None
 
diff --git a/releasenotes/notes/feat-widen-support-of-envars-vars-on-openai-components-efe6203c0c6bd7b3.yaml b/releasenotes/notes/feat-widen-support-of-envars-vars-on-openai-components-efe6203c0c6bd7b3.yaml
new file mode 100644
index 00000000..acc1d7fc
--- /dev/null
+++ b/releasenotes/notes/feat-widen-support-of-envars-vars-on-openai-components-efe6203c0c6bd7b3.yaml
@@ -0,0 +1,9 @@
+---
+highlights: >
+    Add the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' to the OpenAI components.
+enhancements:
+  - |
+    Now you can set the timeout and max_retries parameters on OpenAI components by setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' environment vars or passing them at __init__.
+issues:
+  - |
+    7610


Test patch:
diff --git a/test/components/embedders/test_openai_document_embedder.py b/test/components/embedders/test_openai_document_embedder.py
index 0995cb29..f64a3aca 100644
--- a/test/components/embedders/test_openai_document_embedder.py
+++ b/test/components/embedders/test_openai_document_embedder.py
@@ -29,6 +29,7 @@ class TestOpenAIDocumentEmbedder:
     def test_init_default(self, monkeypatch):
         monkeypatch.setenv("OPENAI_API_KEY", "fake-api-key")
         embedder = OpenAIDocumentEmbedder()
+        assert embedder.api_key.resolve_value() == "fake-api-key"
         assert embedder.model == "text-embedding-ada-002"
         assert embedder.organization is None
         assert embedder.prefix == ""
@@ -37,10 +38,14 @@ class TestOpenAIDocumentEmbedder:
         assert embedder.progress_bar is True
         assert embedder.meta_fields_to_embed == []
         assert embedder.embedding_separator == "\n"
+        assert embedder.client.max_retries == 5
+        assert embedder.client.timeout == 30.0
 
-    def test_init_with_parameters(self):
+    def test_init_with_parameters(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
         embedder = OpenAIDocumentEmbedder(
-            api_key=Secret.from_token("fake-api-key"),
+            api_key=Secret.from_token("fake-api-key-2"),
             model="model",
             organization="my-org",
             prefix="prefix",
@@ -49,7 +54,10 @@ class TestOpenAIDocumentEmbedder:
             progress_bar=False,
             meta_fields_to_embed=["test_field"],
             embedding_separator=" | ",
+            timeout=40.0,
+            max_retries=1,
         )
+        assert embedder.api_key.resolve_value() == "fake-api-key-2"
         assert embedder.organization == "my-org"
         assert embedder.model == "model"
         assert embedder.prefix == "prefix"
@@ -58,6 +66,34 @@ class TestOpenAIDocumentEmbedder:
         assert embedder.progress_bar is False
         assert embedder.meta_fields_to_embed == ["test_field"]
         assert embedder.embedding_separator == " | "
+        assert embedder.client.max_retries == 1
+        assert embedder.client.timeout == 40.0
+
+    def test_init_with_parameters_and_env_vars(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
+        embedder = OpenAIDocumentEmbedder(
+            api_key=Secret.from_token("fake-api-key-2"),
+            model="model",
+            organization="my-org",
+            prefix="prefix",
+            suffix="suffix",
+            batch_size=64,
+            progress_bar=False,
+            meta_fields_to_embed=["test_field"],
+            embedding_separator=" | ",
+        )
+        assert embedder.api_key.resolve_value() == "fake-api-key-2"
+        assert embedder.organization == "my-org"
+        assert embedder.model == "model"
+        assert embedder.prefix == "prefix"
+        assert embedder.suffix == "suffix"
+        assert embedder.batch_size == 64
+        assert embedder.progress_bar is False
+        assert embedder.meta_fields_to_embed == ["test_field"]
+        assert embedder.embedding_separator == " | "
+        assert embedder.client.max_retries == 10
+        assert embedder.client.timeout == 100.0
 
     def test_init_fail_wo_api_key(self, monkeypatch):
         monkeypatch.delenv("OPENAI_API_KEY", raising=False)
diff --git a/test/components/embedders/test_openai_text_embedder.py b/test/components/embedders/test_openai_text_embedder.py
index c7848676..31a03605 100644
--- a/test/components/embedders/test_openai_text_embedder.py
+++ b/test/components/embedders/test_openai_text_embedder.py
@@ -20,8 +20,12 @@ class TestOpenAITextEmbedder:
         assert embedder.organization is None
         assert embedder.prefix == ""
         assert embedder.suffix == ""
+        assert embedder.client.timeout == 30
+        assert embedder.client.max_retries == 5
 
-    def test_init_with_parameters(self):
+    def test_init_with_parameters(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
         embedder = OpenAITextEmbedder(
             api_key=Secret.from_token("fake-api-key"),
             model="model",
@@ -29,6 +33,8 @@ class TestOpenAITextEmbedder:
             organization="fake-organization",
             prefix="prefix",
             suffix="suffix",
+            timeout=40.0,
+            max_retries=1,
         )
         assert embedder.client.api_key == "fake-api-key"
         assert embedder.model == "model"
@@ -36,6 +42,28 @@ class TestOpenAITextEmbedder:
         assert embedder.organization == "fake-organization"
         assert embedder.prefix == "prefix"
         assert embedder.suffix == "suffix"
+        assert embedder.client.timeout == 40.0
+        assert embedder.client.max_retries == 1
+
+    def test_init_with_parameters_and_env_vars(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
+        embedder = OpenAITextEmbedder(
+            api_key=Secret.from_token("fake-api-key"),
+            model="model",
+            api_base_url="https://my-custom-base-url.com",
+            organization="fake-organization",
+            prefix="prefix",
+            suffix="suffix",
+        )
+        assert embedder.client.api_key == "fake-api-key"
+        assert embedder.model == "model"
+        assert embedder.api_base_url == "https://my-custom-base-url.com"
+        assert embedder.organization == "fake-organization"
+        assert embedder.prefix == "prefix"
+        assert embedder.suffix == "suffix"
+        assert embedder.client.timeout == 100.0
+        assert embedder.client.max_retries == 10
 
     def test_init_fail_wo_api_key(self, monkeypatch):
         monkeypatch.delenv("OPENAI_API_KEY", raising=False)
diff --git a/test/components/generators/chat/test_openai.py b/test/components/generators/chat/test_openai.py
index 8f40c2fd..e9451217 100644
--- a/test/components/generators/chat/test_openai.py
+++ b/test/components/generators/chat/test_openai.py
@@ -29,24 +29,49 @@ class TestOpenAIChatGenerator:
         assert component.model == "gpt-3.5-turbo"
         assert component.streaming_callback is None
         assert not component.generation_kwargs
+        assert component.client.timeout == 30
+        assert component.client.max_retries == 5
 
     def test_init_fail_wo_api_key(self, monkeypatch):
         monkeypatch.delenv("OPENAI_API_KEY", raising=False)
         with pytest.raises(ValueError, match="None of the .* environment variables are set"):
             OpenAIChatGenerator()
 
-    def test_init_with_parameters(self):
+    def test_init_with_parameters(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
         component = OpenAIChatGenerator(
             api_key=Secret.from_token("test-api-key"),
             model="gpt-4",
             streaming_callback=print_streaming_chunk,
             api_base_url="test-base-url",
             generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
+            timeout=40.0,
+            max_retries=1,
         )
         assert component.client.api_key == "test-api-key"
         assert component.model == "gpt-4"
         assert component.streaming_callback is print_streaming_chunk
         assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
+        assert component.client.timeout == 40.0
+        assert component.client.max_retries == 1
+
+    def test_init_with_parameters_and_env_vars(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
+        component = OpenAIChatGenerator(
+            api_key=Secret.from_token("test-api-key"),
+            model="gpt-4",
+            streaming_callback=print_streaming_chunk,
+            api_base_url="test-base-url",
+            generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
+        )
+        assert component.client.api_key == "test-api-key"
+        assert component.model == "gpt-4"
+        assert component.streaming_callback is print_streaming_chunk
+        assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
+        assert component.client.timeout == 100.0
+        assert component.client.max_retries == 10
 
     def test_to_dict_default(self, monkeypatch):
         monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")
diff --git a/test/components/generators/test_openai.py b/test/components/generators/test_openai.py
index 714c8076..6fe80721 100644
--- a/test/components/generators/test_openai.py
+++ b/test/components/generators/test_openai.py
@@ -22,24 +22,49 @@ class TestOpenAIGenerator:
         assert component.model == "gpt-3.5-turbo"
         assert component.streaming_callback is None
         assert not component.generation_kwargs
+        assert component.client.timeout == 30
+        assert component.client.max_retries == 5
 
     def test_init_fail_wo_api_key(self, monkeypatch):
         monkeypatch.delenv("OPENAI_API_KEY", raising=False)
         with pytest.raises(ValueError, match="None of the .* environment variables are set"):
             OpenAIGenerator()
 
-    def test_init_with_parameters(self):
+    def test_init_with_parameters(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
         component = OpenAIGenerator(
             api_key=Secret.from_token("test-api-key"),
             model="gpt-4",
             streaming_callback=print_streaming_chunk,
             api_base_url="test-base-url",
             generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
+            timeout=40.0,
+            max_retries=1,
         )
         assert component.client.api_key == "test-api-key"
         assert component.model == "gpt-4"
         assert component.streaming_callback is print_streaming_chunk
         assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
+        assert component.client.timeout == 40.0
+        assert component.client.max_retries == 1
+
+    def test_init_with_parameters(self, monkeypatch):
+        monkeypatch.setenv("OPENAI_TIMEOUT", "100")
+        monkeypatch.setenv("OPENAI_MAX_RETRIES", "10")
+        component = OpenAIGenerator(
+            api_key=Secret.from_token("test-api-key"),
+            model="gpt-4",
+            streaming_callback=print_streaming_chunk,
+            api_base_url="test-base-url",
+            generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
+        )
+        assert component.client.api_key == "test-api-key"
+        assert component.model == "gpt-4"
+        assert component.streaming_callback is print_streaming_chunk
+        assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
+        assert component.client.timeout == 100.0
+        assert component.client.max_retries == 10
 
     def test_to_dict_default(self, monkeypatch):
         monkeypatch.setenv("OPENAI_API_KEY", "test-api-key")