langchain-ai / langchain-google

MIT License
74 stars 78 forks source link

Breaking change in langchain-google-genai after version from 1.0.3 -> 1.0.4 or higher gives 'TypeError: generate_content() got an unexpected keyword argument 'request' #296

Closed namitharajb closed 1 week ago

namitharajb commented 2 weeks ago

Hi, We are using langchain-google-genai in our AI sdk and for langchain integration of gemini model, we use langchain-google-genai, which was working fine with version 1.0.3 and as soon as I update it to version 1.0.4 or higher I get below error from chat_models.py:187: in _chat_with_retry.

Previously we had following library version:

langchain~=0.1.16 
google-generativeai~=0.5.4 
langchain-google-genai==1.0.3 

now when I update to :

langchain[openai]~=0.2.1 
google-generativeai~=0.5.4 
langchain-google-genai==1.0.4

This request parameter is added in https://github.com/langchain-ai/langchain-google/blob/f11446a743f104703ddd9bac811a4527a85ae90a/libs/genai/langchain_google_genai/chat_models.py#L760

The version 1.0.3 worked as expected but not in version 1.0.4 or higher and I see there was a refactoring done, could you please let me know if there is any change in the way we invoke the chain or initialize the model is exactly like before.

It looks like the refactoring https://github.com/langchain-ai/langchain-google/pull/209/files#diff-789e24521c3e983e4ad9f51a1c0db73ea6fe0e36a60609b2d20026b106c4b813 has caused breaking changes.

chat_model = ChatGoogleGenerativeAI(proxy_model_name='gemini-1.0-pro', proxy_client=self.proxy_client) self.assertIsNotNone(chat_model.model) tweet_prompt = PromptTemplate.from_template("You are a content creator. Write me a tweet about {topic}.") chain = LLMChain(llm=chat_model, prompt=tweet_prompt, verbose=True)

  response = chain.invoke('How does it feel to experience snow in summer?')

test_google.py:44:


../../ENV/lib/python3.9/site-packages/langchain/chains/base.py:166: in invoke raise e ../../ENV/lib/python3.9/site-packages/langchain/chains/base.py:156: in invoke self._call(inputs, run_manager=run_manager) ../../ENV/lib/python3.9/site-packages/langchain/chains/llm.py:126: in _call response = self.generate([inputs], run_manager=run_manager) ../../ENV/lib/python3.9/site-packages/langchain/chains/llm.py:138: in generate return self.llm.generate_prompt( ../../ENV/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:599: in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, kwargs) ../../ENV/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:456: in generate raise e ../../ENV/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:446: in generate self._generate_with_cache( ../../ENV/lib/python3.9/site-packages/langchain_core/language_models/chat_models.py:671: in _generate_with_cache result = self._generate( ../../ENV/lib/python3.9/site-packages/langchain_google_genai/chat_models.py:760: in _generate response: GenerateContentResponse = _chat_with_retry( ../../ENV/lib/python3.9/site-packages/langchain_google_genai/chat_models.py:189: in _chat_with_retry return _chat_with_retry(kwargs) ../../ENV/lib/python3.9/site-packages/tenacity/init.py:289: in wrapped_f return self(f, *args, *kw) ../../ENV/lib/python3.9/site-packages/tenacity/init.py:379: in call do = self.iter(retry_state=retry_state) ../../ENV/lib/python3.9/site-packages/tenacity/init.py:314: in iter return fut.result() /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py:438: in result return self.get_result() /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py:390: in get_result raise self._exception ../../ENV/lib/python3.9/site-packages/tenacity/init.py:382: in call result = fn(args, **kwargs) ../../ENV/lib/python3.9/site-packages/langchain_google_genai/chat_models.py:187: in _chat_with_retry raise e


kwargs = {'metadata': [], 'request': model: "gemini-1.0-pro" contents { parts { text: "You are a content creator. Write m...loods affect Heidelberg, Germany." } role: "user" } generation_config { candidate_count: 1 temperature: 0.7 } }

@retry_decorator
def _chat_with_retry(**kwargs: Any) -> Any:
    try:
      return generation_method(**kwargs)

E TypeError: generate_content() got an unexpected keyword argument 'request'

../../ENV/lib/python3.9/site-packages/langchain_google_genai/chat_models.py:171: TypeError

namitharajb commented 2 weeks ago

@lkuligin I see that the refactoring was done by you. It would be great if you could take a look at this issue as this is blocking us from using latest langchain and langchain-google versions.

lkuligin commented 1 week ago

It looks like you're doing some additional transformations on your side.

This code works fine with the latest version of libraries:

from langchain_google_genai import ChatGoogleGenerativeAI

llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=api_key)

from langchain_core.prompts.prompt import PromptTemplate
from langchain.chains import LLMChain

tweet_prompt = PromptTemplate.from_template("You are a content creator. Write me a tweet about {topic}.")
chain = LLMChain(llm=llm, prompt=tweet_prompt, verbose=True)

response = chain.invoke('How does it feel to experience snow in summer?')
print(response)
lkuligin commented 1 week ago

so essentially there're no breaking changes to the standard interfaces, if you're using low-level private methods, maybe you need to adjust things on your side accordingly, but I'm not sure it can be treated as a "breaking change".

namitharajb commented 1 week ago

@lkuligin There hasn't been any code changes in our sdk where we use langchain-google, the above error is caused only when I upgrade the library to version 1.0.4 or higher. If I use 1.0.3 the "invoke" call works fine.

The argument "request" in the error is also something that is added within langchain_google_genai and not from our code.

E TypeError: generate_content() got an unexpected keyword argument 'request'

../../ENV/lib/python3.9/site-packages/langchain_google_genai/chat_models.py:171: TypeError

This request attribute is set within https://github.com/langchain-ai/langchain-google/blob/f11446a743f104703ddd9bac811a4527a85ae90a/libs/genai/langchain_google_genai/chat_models.py#L760

namitharajb commented 1 week ago

Which is the compatible version of google-generativeai library for the latest version 1.0.6 of langchain-google-genai? Because it seems to complain if I try to use latest version of google-generativeai (0.7.0).