Skip to content

Commit

Permalink
Fix response schema generator (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade authored Aug 31, 2024
1 parent 9826a9e commit a06dac0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
43 changes: 35 additions & 8 deletions python/composio/tools/base/abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,13 @@ def schema(self) -> t.Dict:
del prop["type"] # Remove original type to avoid conflict in oneOf
continue

if (
"allOf" in prop
and len(prop["allOf"]) == 1
and "enum" in prop["allOf"][0]
):
if "allOf" in prop and len(prop["allOf"]) == 1:
(schema,) = prop.pop("allOf")
prop.update(schema)
prop[
"description"
] += f" Note: choose value only from following options - {prop['enum']}"
if "enum" in schema:
prop[
"description"
] += f" Note: choose value only from following options - {prop['enum']}"

request["properties"] = properties
return request
Expand Down Expand Up @@ -170,6 +167,36 @@ class wrapper(model): # type: ignore
def schema(self) -> t.Dict:
"""Build request schema."""
schema = self.wrapper.model_json_schema(by_alias=True)
schema = remove_json_ref(schema)
if "$defs" in schema:
del schema["$defs"]

properties = schema.get("properties", {})
for prop in properties.values():
if prop.get("file_readable", False):
prop["oneOf"] = [
{
"type": prop.get("type"),
"description": prop.get("description", ""),
},
{
"type": "string",
"format": "file-path",
"description": f"File path to {prop.get('description', '')}",
},
]
del prop["type"] # Remove original type to avoid conflict in oneOf
continue

if "allOf" in prop and len(prop["allOf"]) == 1:
(schema,) = prop.pop("allOf")
prop.update(schema)
if "enum" in schema:
prop[
"description"
] += f" Note: choose value only from following options - {prop['enum']}"

schema["properties"] = properties
schema["title"] = self.model.__name__
return remove_json_ref(schema)

Expand Down
1 change: 1 addition & 0 deletions python/composio/utils/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def pydantic_model_from_param_schema(param_schema: t.Dict) -> t.Type:
optional_fields = {}
if "title" not in param_schema:
raise ValueError(f"Missing 'title' in param_schema: {param_schema}")

param_title = str(param_schema["title"]).replace(" ", "")
required_props = param_schema.get("required", [])

Expand Down

0 comments on commit a06dac0

Please sign in to comment.