How to I pass a body for API chain if the body is an array? #9514
Replies: 1 comment 5 replies
-
🤖 Hello, From your description, it seems like you're trying to pass an array as the body of your API request. The The The Here is the relevant code: @classmethod
def from_api_operation(
cls,
operation: APIOperation,
llm: BaseLanguageModel,
requests: Optional[Requests] = None,
verbose: bool = False,
return_intermediate_steps: bool = False,
raw_response: bool = False,
callbacks: Callbacks = None,
**kwargs: Any
# TODO: Handle async
) -> "OpenAPIEndpointChain":
"""Create an OpenAPIEndpointChain from an operation and a spec."""
param_mapping = _ParamMapping(
query_params=operation.query_params,
body_params=operation.body_params,
path_params=operation.path_params,
)
...
return cls(
...
param_mapping=param_mapping,
...
)
def _extract_body_params(self, args: Dict[str, str]) -> Optional[Dict[str, str]]:
"""Extract the request body params from the deserialized input."""
body_params = None
if self.param_mapping.body_params:
body_params = {}
for param in self.param_mapping.body_params:
if param in args:
body_params[param] = args.pop(param)
return body_params This method does not differentiate between single values and arrays. If a body parameter is an array, it will be included in the In your case, you should be able to pass the array as the body parameter like this: output = chain(f"What is the API response for text = [{'text': 'gandhi'}]") This should correctly pass the array I hope this helps! Let me know if you have any other questions. SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using -
chain = OpenAPIEndpointChain.from_api_operation(
operation,
llm,
Requests(),
)
print("Chain is ", chain)
output = chain(f"What is the API response for text =grey")
and I want to hit the API with following body
[{'text': 'gandhi'}]
Following are the intermediate_steps i get -
OutPut: {'instructions': 'What is the API resposne? for text =grey', 'output': 'The API could not understand the request you sent. It returned a 400 Bad Request error. Please make sure to provide the correct parameters for your request and try again.', 'intermediate_steps': {'request_args': '{"body": ["grey"], "x-correlation-id": "", "accept-language": "en"}', 'response_text': '400: Bad Request\nFor POST https://translation-pp.monotype.com/translation/v2\nCalled with args: {}'}}
{'request_args': '{"body": ["grey"], "x-correlation-id": "", "accept-language": "en"}', 'response_text': '400: Bad Request\nFor POST https://translation-pp.monotype.com/translation/v2\nCalled with args: {}'}
API Response 400: Bad Request
This indicates wrong body sent.
Also, If I send output = chain(f"What is the API response for [{"text" ="grey"}])
Then 'request_args': '{"body": [{"text": "gandhi"}], "x-correlation-id": "", "accept-language": "en"}'
Beta Was this translation helpful? Give feedback.
All reactions