langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Mypy: Missing named argument "client" for "ChatOpenAI"

Open ryanpeach opened this issue 1 year ago • 9 comments

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?

ryanpeach avatar Apr 15 '23 03:04 ryanpeach

+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)

Ben-Epstein avatar May 03 '23 14:05 Ben-Epstein

Same issue.

Any idea how to go around it?

jakublala avatar May 23 '23 13:05 jakublala

For now I am just doing #type: ignore in the line where the error is encountered, but is there a plan to fix it?

vikramsg avatar Jun 05 '23 19:06 vikramsg

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'.

squat avatar Jun 06 '23 13:06 squat

I'm still getting this error. Any more information about how to fix this?

dcgudeman avatar Jun 14 '23 19:06 dcgudeman

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

cmrfrd avatar Jun 14 '23 20:06 cmrfrd

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.

DHubbert avatar Jun 27 '23 10:06 DHubbert

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)?

jphme avatar Jul 05 '23 11:07 jphme

Another hack: Use

ChatOpenAI(model="gpt-4", temperature=temperature, client=openai.ChatCompletion)

Apparently, ChatOpenAI always sets client to openai.ChatCompletion anyway.

UmerHA avatar Jul 11 '23 13:07 UmerHA

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.

dosubot[bot] avatar Oct 10 '23 16:10 dosubot[bot]

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

codekiln avatar Apr 18 '24 20:04 codekiln