Skip to content

Commit 5259940

Browse files
committed
Copy full JSON schema to OpenAI function
This works around the bug where array types get parsed as object types
1 parent 511b45d commit 5259940

File tree

1 file changed

+6
-47
lines changed

1 file changed

+6
-47
lines changed

langchain/src/chains/openai_functions/openapi.ts

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ function convertOpenAPIParamsToJSONSchema(
107107
if (param.schema) {
108108
schema = spec.getSchema(param.schema);
109109
// eslint-disable-next-line no-param-reassign
110-
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(
111-
schema,
112-
spec
113-
);
110+
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(schema);
114111
} else if (param.content) {
115112
const mediaTypeSchema = Object.values(param.content)[0].schema;
116113
if (mediaTypeSchema) {
@@ -123,10 +120,7 @@ function convertOpenAPIParamsToJSONSchema(
123120
schema.description = param.description ?? "";
124121
}
125122
// eslint-disable-next-line no-param-reassign
126-
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(
127-
schema,
128-
spec
129-
);
123+
jsonSchema.properties[param.name] = convertOpenAPISchemaToJSONSchema(schema);
130124
} else {
131125
return jsonSchema;
132126
}
@@ -152,41 +146,9 @@ function convertOpenAPIParamsToJSONSchema(
152146
* @returns The JSON schema representation of the OpenAPI schema.
153147
*/
154148
function convertOpenAPISchemaToJSONSchema(
155-
schema: OpenAPIV3_1.SchemaObject,
156-
spec: OpenAPISpec
157-
) {
158-
if (schema.type !== "object" && schema.type !== "array") {
159-
return {
160-
type: schema.type ?? "string",
161-
} as JsonSchema7Type;
162-
}
163-
return Object.keys(schema.properties ?? {}).reduce(
164-
(jsonSchema: JsonSchema7ObjectType, propertyName) => {
165-
if (!schema.properties) {
166-
return jsonSchema;
167-
}
168-
const openAPIProperty = spec.getSchema(schema.properties[propertyName]);
169-
if (openAPIProperty.type === undefined) {
170-
return jsonSchema;
171-
}
172-
// eslint-disable-next-line no-param-reassign
173-
jsonSchema.properties[propertyName] = {
174-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
175-
type: openAPIProperty.type as any,
176-
description: openAPIProperty.description,
177-
};
178-
if (openAPIProperty.required && jsonSchema.required !== undefined) {
179-
jsonSchema.required.push(propertyName);
180-
}
181-
return jsonSchema;
182-
},
183-
{
184-
type: "object",
185-
properties: {},
186-
required: [],
187-
additionalProperties: {},
188-
}
189-
);
149+
schema: OpenAPIV3_1.SchemaObject
150+
): JsonSchema7Type {
151+
return schema;
190152
}
191153

192154
/**
@@ -258,10 +220,7 @@ function convertOpenAPISpecToOpenAIFunctions(spec: OpenAPISpec): {
258220
)) {
259221
if (mediaTypeObject.schema !== undefined) {
260222
const schema = spec.getSchema(mediaTypeObject.schema);
261-
requestBodySchemas[mediaType] = convertOpenAPISchemaToJSONSchema(
262-
schema,
263-
spec
264-
) as JsonSchema7ObjectType;
223+
requestBodySchemas[mediaType] = convertOpenAPISchemaToJSONSchema(schema) as JsonSchema7ObjectType;
265224
}
266225
}
267226
const mediaTypes = Object.keys(requestBodySchemas);

0 commit comments

Comments
 (0)