langchain
langchain copied to clipboard
Mypy: Missing named argument "client" for "ChatOpenAI"
This is the mypy response for the following code:
ChatOpenAI(
model_name=args.model_name,
temperature=args.temperature,
)
I see in the code that ChatOpenAI has a variable client that in the comments is marked private.
Any remediation?
+1 seeing the same
If you try to add a client
param, you see
---> 10 conversation = ConversationChain(
11 llm=llm,
12 verbose=True,
13 memory=memory,
14 client=None,
15 )
File ~/site-packages/pydantic/main.py:341, in pydantic.main.BaseModel.__init__()
ValidationError: 1 validation error for ConversationChain
client
extra fields not permitted (type=value_error.extra)
Same issue.
Any idea how to go around it?
For now I am just doing #type: ignore
in the line where the error is encountered, but is there a plan to fix it?
This is a bit annoying, One way around this without using #type: ignore
is to use .construct()
instead, and in this way do the instantiation of the model without validation [0]. This is also not great because it throws pydantic's guarantees out the window, just as ignoring types throws mypy's guarantees out the window.
[0] https://docs.pydantic.dev/latest/usage/models/#creating-models-without-validation
Edit: This actually does not work! Because of how langchain instantiates the client in the model, the client
field is instantiated in the root_validator
so if you use construct()
and thus bypass validation, no instantiation of the client field will occur and the instance will have errors like AttributeError: 'NoneType' object has no attribute 'create'
.
I'm still getting this error. Any more information about how to fix this?
This is a quick hack to get around this.
test.py
from typing import Any, Optional
from langchain.chat_models import ChatOpenAI
class WrappedChatOpenAI(ChatOpenAI):
client: Optional[Any] = None
print(WrappedChatOpenAI().predict(text="Hello!"))
and I can get mypy to pass this
$ mypy test.py
Success: no issues found in 1 source file
This is a quick hack to get around this.
test.py
from typing import Any, Optional from langchain.chat_models import ChatOpenAI class WrappedChatOpenAI(ChatOpenAI): client: Optional[Any] = None print(WrappedChatOpenAI().predict(text="Hello!"))
and I can get mypy to pass this
$ mypy test.py Success: no issues found in 1 source file
I just had the same problem with OpenAIEmbeddings, and this workaround worked for that as well. Thanks.
I have the same issue (which is not really critical but annoying) - any possibility to fix it in the underlying code (e.g. with some comment in the client
line)?
Another hack: Use
ChatOpenAI(model="gpt-4", temperature=temperature, client=openai.ChatCompletion)
Apparently, ChatOpenAI
always sets client
to openai.ChatCompletion
anyway.
Hi, @ryanpeach. I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.
Based on my understanding, the issue is about a missing named argument "client" for the "ChatOpenAI" class in the Mypy response. It seems that the "client" variable is marked as private in the code, and you were looking for a solution. I noticed that several users have reported the same issue and have suggested workarounds such as using #type: ignore
, using .construct()
instead of validation, or creating a wrapped class with the missing argument. One user also mentioned that using openai.ChatCompletion
as the value for the "client" argument could be a workaround.
Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.
Thank you for your understanding and contribution to the LangChain project. Let us know if you have any further questions or concerns.
I used this ...
This is a quick hack to get around this.
test.py
from typing import Any, Optional from langchain.chat_models import ChatOpenAI class WrappedChatOpenAI(ChatOpenAI): client: Optional[Any] = None print(WrappedChatOpenAI().predict(text="Hello!"))
and I can get mypy to pass this
$ mypy test.py Success: no issues found in 1 source file
As a workaround for the same issue for BedrockChat
:
class WrappedBedrockChat(BedrockChat):
"""
This is a workaround for for a mypy failure
error: Missing named argument "client" for "BedrockChat" [call-arg]
"""
client: Optional[Any] = None