Skip to content

Commit

Permalink
langchain[patch]: Take all required fields into account for OpenAPI c…
Browse files Browse the repository at this point in the history
…hain (#6700)

Co-authored-by: Brace Sproul <braceasproul@gmail.com>
  • Loading branch information
ikalachy and bracesproul authored Sep 5, 2024
1 parent da42b13 commit f0a6094
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion langchain/src/chains/openai_functions/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions langchain/src/chains/openai_functions/tests/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f0a6094

Please sign in to comment.