Getting an error while trying to create an LLM component

The code I am executing is

llm_generator = OpenAIGenerator(api_key=my_key_str)

The error I am getting is

AttributeError: ‘str’ object has no attribute ‘resolve_value’

The code that is producing the error is

125 self.client = OpenAI(
→ 126 api_key=api_key.resolve_value(),
127 organization=organization,
128 base_url=api_base_url,

Any idea why this is happening?

Hey @rajivs - This is because we have a safety measure in place which means we ask that api keys are provided via the Secret dataclass.
Long story short, this means you don’t risk having your api key exposed as a bare string when/if you serialize your pipeline to something like YAML.

I tried to add the link to the docs but the platform wont allow me. So go to Haystack docs → Haystack Concepts → Secret Management

And here’s how you could resolve that issue:

llm_generator = OpenAIGenerator(api_key=Secret.from_token("your token string"))