langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
33.63k stars 4.08k forks source link

APIRequest response data types getting mangled in Data object #4472

Open mieslep opened 3 hours ago

mieslep commented 3 hours ago

Bug Description

Server requests come back and the .json() values have correct types (numbers, booleans, strings), but in the component output all of the values are converted to string.

Reproduction

This flow uses postman-echo.com to send a request body which is echoed back, and includes some logger.debug statements:

API RequestWithDebug.json

And we can see from the log outputs, the numbers and booleans are not wrapped in ":

                    DEBUG    2024-11-08 15:48:37 - DEBUG    - <string> - request.json: {'name': 'John Doe', 'age': 30, 'as_of': '2024-11-08T14:30:00Z', 'alive': True,   <string>:131
                             'net_worth': 123.45}
[11/08/24 15:48:38] DEBUG    2024-11-08 15:48:38 - DEBUG    - <string> - result: {'args': {}, 'data': {'name': 'John Doe', 'age': 30, 'as_of': '2024-11-08T14:30:00Z',   <string>:135
                             'alive': True, 'net_worth': 123.45}, 'files': {}, 'form': {}, 'headers': {'host': 'postman-echo.com', 'x-request-start':
                             't=1731080917.687', 'connection': 'close', 'content-length': '100', 'x-forwarded-proto': 'https', 'x-forwarded-port': '443',
                             'x-amzn-trace-id': 'Root=1-672e32d5-1e222a7f47d068254a1c7cb9', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'user-agent':
                             'python-httpx/0.27.0', 'content-type': 'application/json'}, 'json': {'name': 'John Doe', 'age': 30, 'as_of': '2024-11-08T14:30:00Z',
                             'alive': True, 'net_worth': 123.45}, 'url': 'https://postman-echo.com/post'}

However if you inspect the output record HTML, the response value has converted these to strings:

{"args":{},"data":{"name":"John Doe","age":"30","as_of":"2024-11-08T14:30:00Z","alive":"True","net_worth":"123.45"},"files":{},"form":{},"headers":{"host":"postman-echo.com","x-request-start":"t=1731080917.687","connection":"close","content-length":"100","x-forwarded-proto":"https","x-forwarded-port":"443","x-amzn-trace-id":"Root=1-672e32d5-1e222a7f47d068254a1c7cb9","accept":"*/*","accept-encoding":"gzip, deflate","user-agent":"python-httpx/0.27.0","content-type":"application/json"},"json":{"name":"John Doe","age":"30","as_of":"2024-11-08T14:30:00Z","alive":"True","net_worth":"123.45"},"url":"https://postman-echo.com/post"}

And in the new dict viewer the values are string.

Expected behavior

Data types should be as they were in the JSON response.

Who can help?

No response

Operating System

Windows

Langflow Version

v1.0.19.dev24

Python Version

3.12

Screenshot

No response

Flow File

API Reponse Debug.json

dosubot[bot] commented 3 hours ago

The issue you're experiencing, where data types in the APIRequest response are being converted to strings in the component output, is likely due to the serialization process in the make_request method. This method uses json.dumps(data) to serialize the request body into a JSON-formatted string, which converts all data types into their string representations [1].

To address this, you might need to review how the data is being processed after the response is received. The make_request method attempts to parse the response content as JSON and stores it in the result field of the Data object. If the JSON parsing fails, it falls back to storing the response text [1].

Additionally, ensure that any further processing or rendering of the data does not inadvertently convert the data types to strings. You might want to check if there are any specific methods or functions in the codebase that explicitly convert non-string data types to strings, such as data_to_string, document_to_string, or dict_values_to_string [2].

To continue talking to Dosu, mention @dosu.

mieslep commented 3 hours ago

I suspect the problem is in how the Data object is deserializing the result:

            response = await client.request(method, url, headers=headers, json=data, timeout=timeout)
            try:
                result = response.json()
            except Exception:  # noqa: BLE001
                result = response.text
            return Data(
                data={
                    "source": url,
                    "headers": headers,
                    "status_code": response.status_code,
                    "result": result,
                },
            )

but I have not been able to get an environment working to be able to further isolate this.