Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to serialize objects returned by custom resolvers in parameters #2010

Closed
ravi-kumar-pilla opened this issue Jul 30, 2024 · 1 comment · Fixed by #2122
Closed

Unable to serialize objects returned by custom resolvers in parameters #2010

ravi-kumar-pilla opened this issue Jul 30, 2024 · 1 comment · Fixed by #2122
Assignees
Labels
Issue: Bug Report Python Pull requests that update Python code

Comments

@ravi-kumar-pilla
Copy link
Contributor

Description

Originally raised by Erwin - https://kedro-org.slack.com/archives/C03RKP2LW64/p1722343503669499

Context

From the user -

I just discovered that if I use a custom resolver in parameters, (using it in catalog have no problem) kedro-viz die. The object returned is not friendly for kedro-viz

"polars": lambda x: getattr(pl, x)

pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'polars.datatypes.classes.DataTypeClass'>
2024-07-30 09:15:55,489 - uvicorn.error - ERROR - Exception in ASGI application

Any workaround? Could be a bad practice to have parameters of type polars.datatypes.classes.DataTypeClass ?

Steps to Reproduce

Let's say we have the below resolver -

"custom_resolvers": {
        "add": lambda *my_list: sum(my_list),
        "polars": lambda x: getattr(pl, x)
    }

If the parameters have a resolver and used as below -

model_options:
  test_size: "${add:1,2,3}"
  random_state: 3
  data_info: "${polars:Float64}"

Kedro Viz constructs an object like -

parameters={
    'model_options': {
      'test_size': 6,
      'random_state': 3,
      'data_info': Float64,
}

Now Float64 is not serializable by pydantic when it is sent as json to frontend. We either need to make str(Float64) or see what to do in case the parameter value is an object.

Expected Result

Kedro-Viz should handle parameter values of object types and do not crash

Actual Result

Kedro-Viz throws and error and does not start.

pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'polars.datatypes.classes.DataTypeClass'>
2024-07-30 09:15:55,489 - uvicorn.error - ERROR - Exception in ASGI application

Your Environment

Include as many relevant details as possible about the environment you experienced the bug in:

  • Web browser system and version: Chrome
  • Operating system and version: macOS
  • NodeJS version used (if relevant):
  • Kedro version used (if relevant): latest
  • Python version used (if relevant):

Checklist

  • [] Include labels so that we can categorise your issue
@ravi-kumar-pilla
Copy link
Contributor Author

Possible implementations -
Using - https://docs.pydantic.dev/latest/api/functional_serializers/#pydantic.functional_serializers.field_serializer
and handling the parameters_value in ParametersNode at models/flowchart.py

@ravi-kumar-pilla ravi-kumar-pilla added the Python Pull requests that update Python code label Jul 30, 2024
@rashidakanchwala rashidakanchwala moved this to Todo in Kedro-Viz Aug 5, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in Kedro-Viz Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue: Bug Report Python Pull requests that update Python code
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants