-
Notifications
You must be signed in to change notification settings - Fork 48
fix(openapi-converter): stop dropping tool inputs on non-JSON request bodies and content-form parameters #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AmirF194
wants to merge
2
commits into
universal-tool-calling-protocol:main
Choose a base branch
from
AmirF194:fix/98-openapi-converter-input-data-loss
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−15
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,7 +562,7 @@ def _create_tool(self, path: str, method: str, operation: Dict[str, Any], base_u | |
| description = operation.get("summary") or operation.get("description", "") | ||
| tags = operation.get("tags", []) | ||
|
|
||
| inputs, header_fields, body_field = self._extract_inputs(path, operation) | ||
| inputs, header_fields, body_field, body_content_type = self._extract_inputs(path, operation) | ||
| outputs = self._extract_outputs(operation) | ||
| auth = self._extract_auth(operation) | ||
|
|
||
|
|
@@ -575,7 +575,8 @@ def _create_tool(self, path: str, method: str, operation: Dict[str, Any], base_u | |
| url=full_url, | ||
| body_field=body_field if body_field else None, | ||
| header_fields=header_fields if header_fields else None, | ||
| auth=auth | ||
| auth=auth, | ||
| content_type=body_content_type or "application/json" | ||
| ) | ||
|
|
||
| return Tool( | ||
|
|
@@ -587,8 +588,8 @@ def _create_tool(self, path: str, method: str, operation: Dict[str, Any], base_u | |
| tool_call_template=call_template | ||
| ) | ||
|
|
||
| def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSchema, List[str], Optional[str]]: | ||
| """Extracts input schema, header fields, and body field from an OpenAPI operation. | ||
| def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSchema, List[str], Optional[str], Optional[str]]: | ||
| """Extracts input schema, header fields, body field, and body media type from an OpenAPI operation. | ||
|
|
||
| - Merges path-level and operation-level parameters | ||
| - Resolves $ref for parameters | ||
|
|
@@ -598,6 +599,7 @@ def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSch | |
| required = [] | ||
| header_fields = [] | ||
| body_field = None | ||
| body_content_type = None | ||
|
|
||
| # Merge path-level and operation-level parameters | ||
| path_item = self.spec.get("paths", {}).get(path, {}) if path else {} | ||
|
|
@@ -639,6 +641,16 @@ def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSch | |
|
|
||
| # Non-body parameter | ||
| schema = self._resolve_ref_obj(param.get("schema", {}), set()) or {} | ||
| param_content_obj = None | ||
| if not schema and isinstance(param.get("content"), dict): | ||
| # OpenAPI 3.x allows a Parameter Object to carry its schema under a | ||
| # 'content' map (media-type -> Media Type Object) instead of 'schema', | ||
| # e.g. for parameters that need a media type other than the implicit one. | ||
| for media_type_obj_candidate in param["content"].values(): | ||
| if isinstance(media_type_obj_candidate, dict): | ||
| param_content_obj = media_type_obj_candidate | ||
| schema = self._resolve_ref_obj(param_content_obj.get("schema", {}), set()) or {} | ||
| break | ||
| if not schema: | ||
| # OpenAPI 2.0 non-body params use top-level type/items | ||
| if "type" in param: | ||
|
|
@@ -647,10 +659,10 @@ def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSch | |
| schema["items"] = param.get("items") | ||
| if "enum" in param: | ||
| schema["enum"] = param.get("enum") | ||
| # Examples can live on the parameter itself and on its schema; | ||
| # collect both into the normalized 'examples' keyword. | ||
| param_examples = self._merge_examples(param, schema) | ||
|
|
||
| # Examples can live on the parameter itself, its schema, and (for the | ||
| # 'content' form) the Media Type Object; collect all into 'examples'. | ||
| param_examples = self._merge_examples(param, schema, param_content_obj) | ||
|
|
||
| prop = { | ||
| "description": param.get("description", ""), | ||
|
|
@@ -667,12 +679,20 @@ def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSch | |
| request_body = operation.get("requestBody") | ||
| if request_body: | ||
| content = request_body.get("content", {}) | ||
| json_schema = content.get("application/json", {}).get("schema") | ||
| json_schema = self._resolve_ref_obj(json_schema, set()) if json_schema else None | ||
|
|
||
| # Examples can live on the media type object and on the schema; | ||
| # collect both into the normalized 'examples' keyword. | ||
| body_content_type = "application/json" if "application/json" in content else None | ||
| media_type_obj = content.get("application/json", {}) | ||
| json_schema = media_type_obj.get("schema") | ||
| # Fall back to the first schema-bearing media type when the body has no | ||
| # application/json entry (e.g. application/xml-only), matching the | ||
| # fallback _extract_outputs already does for response bodies. | ||
| if json_schema is None and isinstance(content, dict): | ||
| for candidate_media_type, candidate_media_type_obj in content.items(): | ||
| if isinstance(candidate_media_type_obj, dict) and "schema" in candidate_media_type_obj: | ||
| media_type_obj = candidate_media_type_obj | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: When the fallback selects a non-JSON media type, the generated tool still sends Prompt for AI agents |
||
| json_schema = candidate_media_type_obj.get("schema") | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| body_content_type = candidate_media_type | ||
| break | ||
| json_schema = self._resolve_ref_obj(json_schema, set()) if json_schema else None | ||
|
|
||
| if json_schema: | ||
| body_examples = self._merge_examples(media_type_obj, json_schema) | ||
|
|
@@ -686,11 +706,15 @@ def _extract_inputs(self, path: str, operation: Dict[str, Any]) -> Tuple[JsonSch | |
| prop["examples"] = body_examples | ||
|
|
||
| properties[body_field] = prop | ||
| if json_schema.get("required"): | ||
| # requestBody.required (a bool on the request body itself) governs whether | ||
| # the body is a mandatory tool input; json_schema.get("required") is a | ||
| # different thing (the list of the body object's own required properties) | ||
| # and says nothing about whether the body as a whole may be omitted. | ||
| if request_body.get("required"): | ||
| required.append(body_field) | ||
|
|
||
| schema = JsonSchema(properties=properties, required=required if required else None) | ||
| return schema, header_fields, body_field | ||
| return schema, header_fields, body_field, body_content_type | ||
|
|
||
| def _extract_outputs(self, operation: Dict[str, Any]) -> JsonSchema: | ||
| """Extracts the output schema from an OpenAPI operation, resolving refs.""" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: When a content-form query parameter uses
application/json, this branch exposes an object input but drops its serialization metadata. Retain the media type in the generated call template and JSON-serialize that query value.Prompt for AI agents