langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.83k stars 15.12k forks source link

Dall-E Image Generator DallEAPIWrapper Error: openai.AuthenticationError" #27231

Open HonXia opened 1 week ago

HonXia commented 1 week ago

Checked other resources

Example Code

The following Langchain code failed to generate image:

from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

import os

os.environ["OPENAI_API_KEY"] = "********"

from langchain.chains import LLMChain

llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
    input_variables=["image_desc"],
    template="Generate a detailed prompt to generate an image based on the following description: {image_desc}",
)
chain = LLMChain(llm=llm, prompt=prompt)

image_url = DallEAPIWrapper().run(chain.run("halloween night at a haunted museum"))
print(image_url)

The following OpenAI code generated image successfully:

from openai import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "****"

client = OpenAI()

response = client.images.generate(
  model="dall-e-3",
  prompt="a white siamese cat",
  size="1024x1024",
  quality="standard",
  n=1,
)

image_url = response.data[0].url
print(image_url)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last): File "D:\software\JetBrains\PyCharm Community Edition 2024.2.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec pydev_imports.execfile(file, globals, locals) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\software\JetBrains\PyCharm Community Edition 2024.2.3\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "E:\workspace\python-projects\AI-Development\agents\dalle_image.py", line 21, in image_url = DallEAPIWrapper().run(chain.run("halloween night at a haunted museum")) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\workspace\python-projects\AI-Development.venv\Lib\site-packages\langchain_community\utilities\dalle_image_generator.py", line 143, in run response = self.client.generate( ^^^^^^^^^^^^^^^^^^^^^ File "E:\workspace\python-projects\AI-Development.venv\Lib\site-packages\openai\resources\images.py", line 264, in generate return self._post( ^^^^^^^^^^^ File "E:\workspace\python-projects\AI-Development.venv\Lib\site-packages\openai_base_client.py", line 1270, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\workspace\python-projects\AI-Development.venv\Lib\site-packages\openai_base_client.py", line 947, in request return self._request( ^^^^^^^^^^^^^^ File "E:\workspace\python-projects\AI-Development.venv\Lib\site-packages\openai_base_client.py", line 1051, in _request raise self._make_status_error_from_response(err.response) from None openai.AuthenticationError: Error code: 401 - {'error': {'code': 'invalid_api_key', 'message': 'Incorrect API key provided: **. You can find your API key at https://platform.openai.com/account/api-keys.', 'param': None, 'type': 'invalid_request_error'}}

Description

I write a demo following up guideline https://python.langchain.com/docs/integrations/tools/dalle_image_generator/#run-as-a-chain, but it always failed with error :"openai.AuthenticationError: Error code: 401 - {'error': {'code': 'invalid_api_key', 'message': 'Incorrect API key provided: **. You can find your API key at https://platform.openai.com/account/api-keys.', 'param': None, 'type': 'invalid_request_error'}}". I debug the program into _base_client.py found the open AI images generations API url is wrong as the screen capture as below. The same OPENAI_API_KEY can success in the OpenAI sample as above. image

System Info

System Information

OS: Windows OS Version: 10.0.19045 Python Version: 3.12.6 (tags/v3.12.6:a4a2d2b, Sep 6 2024, 20:11:23) [MSC v.1940 64 bit (AMD64)]

Package Information

langchain_core: 0.3.10 langchain: 0.3.3 langchain_community: 0.3.2 langsmith: 0.1.131 langchain_anthropic: 0.2.3 langchain_chroma: 0.1.4 langchain_openai: 0.2.2 langchain_text_splitters: 0.3.0 langgraph: 0.2.34

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.10.9 anthropic: 0.35.0 async-timeout: Installed. No version info available. chromadb: 0.5.11 dataclasses-json: 0.6.7 defusedxml: 0.7.1 fastapi: 0.115.0 httpx: 0.27.2 jsonpatch: 1.33 langgraph-checkpoint: 2.0.0 numpy: 1.26.4 openai: 1.51.0 orjson: 3.10.7 packaging: 24.1 pydantic: 2.9.2 pydantic-settings: 2.5.2 PyYAML: 6.0.2 requests: 2.32.3 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.35 tenacity: 8.5.0 tiktoken: 0.8.0 typing-extensions: 4.12.2

kodychik commented 6 days ago

Hi can I take a look at this issue if no one else is. Thank you

vuhrmeister commented 5 days ago

For me it doesn't even work either.

It seems that the current version 0.2.x has introduced this bug. I was upgrading from 0.1.17 which is still running in production well, but 0.2.x doesn't.

My feeling is that the API key is not used at all. The error message is Incorrect API key provided: **********. whereas if I set an invalid key and try to call the LLM, then the error message is Incorrect API key provided: sk-proj-*********lLAz. (I removed some *** fro shortness). Do you spot the difference?