langchain-ai / langchain

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

AttributeError: 'super' object has no attribute 'parse_obj' when using OpenAPISpec.from_file #9520

Open elliexcoding opened 10 months ago

elliexcoding commented 10 months ago

System Info

Langchain version 0.0.268

Who can help?

No response

Information

Related Components

Reproduction

  1. Load in .yaml file containing the OpenAPI specification.
  2. Call the OpenAPISpec with open_api_spec = OpenAPISpec.from_file(file_path)

Error shows that in openapi.py line 202 ,return super().parse_obj(obj) no longer inherits the super method from Pydantic's BaseModel parse_obj.

Expected behavior

The OpenAPISpec should be delivered so that it can be passed on to NLAToolkit.from_llm_and_spec.

dosubot[bot] commented 10 months ago

🤖

Hello,

The error you're encountering, 'super' object has no attribute 'parse_obj', is likely due to the fact that the 'parse_obj' method is not available in the superclass of 'OpenAPISpec'.

In LangChain, 'OpenAPISpec' is a subclass of 'OpenAPI', which is imported from the 'openapi_schema_pydantic' package. The 'parse_obj' method is called on the superclass in the 'parse_obj' method of 'OpenAPISpec':

@classmethod
def parse_obj(cls, obj: dict) -> OpenAPISpec:
    try:
        cls._alert_unsupported_spec(obj)
        return super().parse_obj(obj)
    except ValidationError as e:
        # We are handling possibly misconfigured specs and
        # want to do a best-effort job to get a reasonable interface out of it.
        new_obj = copy.deepcopy(obj)
        for error in e.errors():
            keys = error["loc"]
            item = new_obj
            for key in keys[:-1]:
                item = item[key]
            item.pop(keys[-1], None)
        return cls.parse_obj(new_obj)

The error suggests that the 'OpenAPI' class does not have a 'parse_obj' method. This could be due to a version mismatch between the 'openapi_schema_pydantic' package used in your environment and the one expected by LangChain.

LangChain v0.0.268 is compatible with the 'openapi_schema_pydantic' package version "^1.2". This means any version that is compatible with version 1.2. The caret (^) symbol in "^1.2" indicates compatibility with versions up to but not including 2.0.0. This information is found in the 'pyproject.toml' file under the '[tool.poetry.dependencies]' section:

openapi-schema-pydantic = {version = "^1.2", optional = true}

Please ensure that you have the correct version of 'openapi_schema_pydantic' installed. If the issue persists, it might be a bug in LangChain or 'openapi_schema_pydantic', and you should consider reporting it to the maintainers.

I hope this helps! Let me know if you have any other questions.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

kreneskyp commented 10 months ago

Also affecting

The get_openapi_chain error is hidden, but I confirmed it's the same error as above.

File "/usr/local/lib/python3.11/site-packages/langchain/chains/openai_functions/openapi.py", line 272, in get_openapi_chain
    raise ValueError(f"Unable to parse spec from source {spec}")
kreneskyp commented 10 months ago

cc @baskaryan @eyurtsev

Tracked this regression to openapi-schema-pydantic being switched to an optional dependency (https://github.com/langchain-ai/langchain/pull/9408).

Work around:

install openapi-schema-pydantic into your environment.

Root cause:

  1. OpenAPISpec's parent class defaults to object when openapi-schema-pydantic is unavailable.

    https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/openapi.py#L54-L57

  2. This leads to an exception in OpenAPISpec.parse_obj when it calls super().parse_obj().

    https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/utilities/openapi.py#L201-L202

batmanscode commented 10 months ago

Thanks for looking into it and figuring out a workaround @kreneskyp

Unfortunately for me installing openapi-schema-pydantic didn't work

EnkrateiaLucca commented 9 months ago

Having the same issue, installing openaiapi-schema-pydantic did not work. :(

kreneskyp commented 8 months ago

Having the same issue, installing openaiapi-schema-pydantic did not work. :(

I ran into this issue again when using Pydantic 2.0 with LangChain.

Try langchain==0.0.319 with openapi-pydantic==0.3.2. Hopefully fixes all the issues now!

PizBernina commented 8 months ago

Having the same issue, installing openaiapi-schema-pydantic did not work. :(

I ran into this issue again when using Pydantic 2.0 with LangChain.

Try langchain==0.0.319 with openapi-pydantic==0.3.2. Hopefully fixes all the issues now!

This resolved the issue for me, cheers!

borisko123 commented 8 months ago

openapi_schema_pydantic is changed to: openapi-pydantic 0.3.2 (last version) install it when using langchain

jepsenwan commented 7 months ago

openapi_schema_pydantic is changed to: openapi-pydantic 0.3.2 (last version) install it when using langchain

This works perfectly fine, thanks for the advise.

marufaytekin commented 6 months ago

openapi_schema_pydantic is changed to: openapi-pydantic 0.3.2 (last version) install it when using langchain

This worked fine, thanks for the advise.

payamsaremi commented 5 months ago

"pip install openapi-pydantic" fixed it for me

xinyangfan commented 3 months ago

langchain 0.1.9 openapi-pydantic 0.4.0 Still having this issue

prova commented 3 months ago

My colleague is facing the same issue on Python 3.10.12 and langchain==0.1.12 plus openapi-pydantic==0.4.0 (and also tried 0.3.2). On my PC, all is good with the same versions and Python. This is some random stuff.

ChanduTadanki commented 3 months ago

openapi_schema_pydantic is changed to: openapi-pydantic 0.3.2 (last version) install it when using langchain

That solved my issue. Thanks. Note: (1) I also had to restart the kernel, (2) remove the imports from openapi_schema_pydantic, and (3) include imports from openapi-pydantic only. More details here: https://community.deeplearning.ai/t/attributeerror-super-object-has-no-attribute-parse-obj/481376/11

bigrobinson commented 6 days ago

Thanks, @ChanduTadanki for reminding me to restart kernel. That was the last step to get things working for me.