Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: api tool encoding (#2296)

Yeuoly6d24a2cb87e3
diff --git a/api/core/tools/tool/api_tool.py b/api/core/tools/tool/api_tool.py
index 4328c7d1e94..442109437a1 100644
--- a/api/core/tools/tool/api_tool.py
+++ b/api/core/tools/tool/api_tool.py
@@ -8,6 +8,7 @@ from core.tools.errors import ToolProviderCredentialValidationError
import httpx
import requests
+import json
class ApiTool(Tool):
api_bundle: ApiBasedToolBundle
@@ -79,11 +80,29 @@ class ApiTool(Tool):
if isinstance(response, httpx.Response):
if response.status_code >= 400:
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
- return response.text
+ if not response.content:
+ return 'Empty response from the tool, please check your parameters and try again.'
+ try:
+ response = response.json()
+ try:
+ return json.dumps(response, ensure_ascii=False)
+ except Exception as e:
+ return json.dumps(response)
+ except Exception as e:
+ return response.text
elif isinstance(response, requests.Response):
if not response.ok:
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
- return response.text
+ if not response.content:
+ return 'Empty response from the tool, please check your parameters and try again.'
+ try:
+ response = response.json()
+ try:
+ return json.dumps(response, ensure_ascii=False)
+ except Exception as e:
+ return json.dumps(response)
+ except Exception as e:
+ return response.text
else:
raise ValueError(f'Invalid response type {type(response)}')
diff --git a/api/core/tools/utils/parser.py b/api/core/tools/utils/parser.py
index f61ac1cbfad..b37ea58a424 100644
--- a/api/core/tools/utils/parser.py
+++ b/api/core/tools/utils/parser.py
@@ -114,6 +114,10 @@ class ApiBasedToolSchemaParser:
if count > 1:
warning['duplicated_parameter'] = f'Parameter {name} is duplicated.'
+ # check if there is a operation id, use $path_$method as operation id if not
+ if 'operationId' not in interface['operation']:
+ interface['operation']['operationId'] = f'{interface["path"]}_{interface["method"]}'
+
bundles.append(ApiBasedToolBundle(
server_url=server_url + interface['path'],
method=interface['method'],
diff --git a/api/services/tools_manage_service.py b/api/services/tools_manage_service.py
index e6348f6ca80..f541957adff 100644
--- a/api/services/tools_manage_service.py
+++ b/api/services/tools_manage_service.py
@@ -485,10 +485,10 @@ class ToolManageService:
if schema_type not in [member.value for member in ApiProviderSchemaType]:
raise ValueError(f'invalid schema type {schema_type}')
- if schema_type == ApiProviderSchemaType.OPENAPI.value:
- tool_bundles = ApiBasedToolSchemaParser.parse_openapi_yaml_to_tool_bundle(schema)
- else:
- raise ValueError(f'invalid schema type {schema_type}')
+ try:
+ tool_bundles, _ = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema)
+ except Exception as e:
+ raise ValueError(f'invalid schema')
# get tool bundle
tool_bundle = next(filter(lambda tb: tb.operation_id == tool_name, tool_bundles), None)