mirror of
https://github.com/deepset-ai/haystack.git
synced 2026-01-07 20:46:31 +00:00
feat: Improve OpenAPIServiceConnector service response serialization (#6772)
* Better service response json -> str serialization * Add unit test
This commit is contained in:
parent
fea1428e84
commit
0b177b3bc6
@ -63,7 +63,12 @@ class OpenAPIServiceConnector:
|
||||
response_messages = []
|
||||
for method_invocation_descriptor in function_invocation_payloads:
|
||||
service_response = self._invoke_method(openapi_service, method_invocation_descriptor)
|
||||
response_messages.append(ChatMessage.from_user(str(service_response)))
|
||||
# openapi3 parses the JSON service response into a model object, which is not our focus at the moment.
|
||||
# Instead, we require direct access to the raw JSON data of the response, rather than the model objects
|
||||
# provided by the openapi3 library. This approach helps us avoid issues related to (de)serialization.
|
||||
# By accessing the raw JSON response through `service_response._raw_data`, we can serialize this data
|
||||
# into a string. Finally, we use this string to create a ChatMessage object.
|
||||
response_messages.append(ChatMessage.from_user(json.dumps(service_response._raw_data)))
|
||||
|
||||
return {"service_response": response_messages}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ import json
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, Mock
|
||||
from openapi3 import OpenAPI
|
||||
from openapi3.schemas import Model
|
||||
from haystack.components.connectors import OpenAPIServiceConnector
|
||||
from haystack.dataclasses import ChatMessage
|
||||
|
||||
@ -79,3 +80,11 @@ class TestOpenAPIServiceConnector:
|
||||
method_invocation_descriptor = {"name": "invalid_method", "arguments": {}}
|
||||
with pytest.raises(RuntimeError):
|
||||
connector._invoke_method(openapi_service_mock, method_invocation_descriptor)
|
||||
|
||||
def test_for_internal_raw_data_field(self):
|
||||
# see https://github.com/deepset-ai/haystack/pull/6772 for details
|
||||
model = Model(data={}, schema={})
|
||||
assert hasattr(model, "_raw_data"), (
|
||||
"openapi3 changed. Model should have a _raw_data field, we rely on it in OpenAPIServiceConnector"
|
||||
" to get the raw data from the service response"
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user