langchain-ai / langchain

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

OpenAPI function : Incorrect payload generated when request body contains Reference #19750

Open anujmehta opened 1 month ago

anujmehta commented 1 month ago

Checked other resources

Example Code

from langchain.chains.openai_functions.openapi import get_openapi_chain

chain = get_openapi_chain("http://localhost:8080/v3/api-docs", llm=llm, verbose=True)
result = chain(question)

Error Message and Stack Trace (if applicable)

No response

Description

I am using openapi functions of langchain so that LLM can invoke appropriate endpoint given OpenAPI 3.0 spec documentation. I had observed that for a POST endpoint if the request body contains a Reference to an object then the schema of the object is not sent to LLM and hence LLM creates incorrect payload.

Below is an example Open API spec where-in the POST endpoint /pet has a request body with reference to Pet Issue is specially for 'address' and 'hobbies' attribute shown in below schema

Screenshot 2024-03-31 at 7 38 21 PM

Below is the Open API 3.0 specs

{ "openapi": "3.0.1", "info": { "title": "OpenAPI definition", "version": "v0" }, "servers": [ { "url": "http://localhost:8080", "description": "Generated server url" } ], "paths": { "/pet": { "post": { "tags": [ "pet-controller" ], "operationId": "createPet", "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Pet" } } }, "required": true }, "responses": { "200": { "description": "OK" } } } } }, "components": { "schemas": { "Address": { "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } } }, "Hobby": { "type": "object", "properties": { "name": { "type": "string" } } }, "Pet": { "type": "object", "properties": { "name": { "type": "string" }, "type": { "type": "string", "enum": [ "CAT", "DOG" ] }, "address": { "$ref": "#/components/schemas/Address" }, "hobbies": { "type": "array", "items": { "$ref": "#/components/schemas/Hobby" } } } } } } }

System Info

langchain = "0.1.9" langchain-community = "0.0.24"

anujmehta commented 1 month ago

PR raised - https://github.com/langchain-ai/langchain/pull/19751