Skip to content

Commit

Permalink
Support empty response
Browse files Browse the repository at this point in the history
  • Loading branch information
mzr1996 committed Mar 13, 2024
1 parent a530755 commit 8e5e96a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions agentlego/tools/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,19 @@ def apply(self, *args, **kwargs):
content = response.content.decode()
raise RuntimeError(f'Failed to call the remote tool `{self.name}` '
f'because of {response.reason}.\nResponse: {content}')

response_schema = self.operation.responses
if response_schema is None or response_schema.get('200') is None:
# Directly use string if the response schema is not specified
return response.text

try:
response = response.json()
except requests.JSONDecodeError as e:
raise RuntimeError(f'Failed to call the remote tool `{self.name}` '
'because of unknown response.\n'
f'Response: {response.content.decode()}') from e

response_schema = self.operation.responses
if response_schema is None or response_schema.get('200') is None:
# Directly use string if the response schema is not specified
return str(response)

out_props = response_schema['200'].properties

if isinstance(out_props, APIResponseProperty):
Expand Down
7 changes: 5 additions & 2 deletions agentlego/utils/openapi/api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,11 @@ def _process_supported_media_type(
return properties

@classmethod
def from_response(cls, response: Response, spec: OpenAPISpec) -> 'APIResponse':
def from_response(cls, response: Response,
spec: OpenAPISpec) -> Optional['APIResponse']:
"""Instantiate from an OpenAPI Response."""
if response.content is None:
return None
# Only handle one potential response payload style.
media_type = next(
(k for k in response.content if k in _SUPPORTED_RESPONSE_MEDIA_TYPES), None)
Expand Down Expand Up @@ -545,7 +548,7 @@ class APIOperation(BaseModel):
request_body: Optional[APIRequestBody] = Field(alias='request_body')
"""The request body of the operation."""

responses: Optional[Dict[str, APIResponse]] = Field(alias='responses')
responses: Optional[Dict[str, Optional[APIResponse]]] = Field(alias='responses')

@staticmethod
def _get_properties_from_parameters(parameters: List[Parameter],
Expand Down

0 comments on commit 8e5e96a

Please sign in to comment.