From 297e11d3fc81a64d9b5321aff1bc7cfe2ecd476a Mon Sep 17 00:00:00 2001 From: Ivan_Kalachyev Date: Thu, 5 Sep 2024 09:53:04 +0300 Subject: [PATCH] fix: take into account all required fields --- .../src/chains/openai_functions/openapi.ts | 6 +++++- .../openai_functions/tests/openapi.test.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/langchain/src/chains/openai_functions/openapi.ts b/langchain/src/chains/openai_functions/openapi.ts index 568ff7635a8c..8b867856f3d8 100644 --- a/langchain/src/chains/openai_functions/openapi.ts +++ b/langchain/src/chains/openai_functions/openapi.ts @@ -175,7 +175,11 @@ export function convertOpenAPISchemaToJSONSchema( openAPIProperty, spec ); - if (openAPIProperty.required && jsonSchema.required !== undefined) { + if ( + (openAPIProperty.required || + schema.required?.includes(propertyName)) && + jsonSchema.required !== undefined + ) { jsonSchema.required.push(propertyName); } return jsonSchema; diff --git a/langchain/src/chains/openai_functions/tests/openapi.test.ts b/langchain/src/chains/openai_functions/tests/openapi.test.ts index d18f755613ce..93243f163b91 100644 --- a/langchain/src/chains/openai_functions/tests/openapi.test.ts +++ b/langchain/src/chains/openai_functions/tests/openapi.test.ts @@ -46,6 +46,19 @@ test("Test convert OpenAPI params to JSON Schema", async () => { }, }, }, + { + name: "objectParamWithRequiredFields", + in: "query", + schema: { + type: "object", + required: ["fooRequired"], + properties: { + fooRequired: { + type: "string", + }, + }, + }, + }, { name: "stringArrayParam", in: "query", @@ -195,6 +208,12 @@ test("Test convert OpenAPI params to JSON Schema", async () => { expectType("string", typedObjectParamSchema.properties.foo); expectType("number", typedObjectParamSchema.properties.bar); + const objectParamWithRequiredFieldSchema = convertOpenAPISchemaToJSONSchema( + getParamSchema(createWidget, "objectParamWithRequiredFields"), + spec + ) as JsonSchema7ObjectType; + expect(objectParamWithRequiredFieldSchema.required).toContain("fooRequired"); + const stringArrayParamSchema = convertOpenAPISchemaToJSONSchema( getParamSchema(createWidget, "stringArrayParam"), spec