question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Python] Unable to deserialize when RestAPI answer is a subtype

See original GitHub issue

In Machine Learning Swagger:

    "WebServiceProperties": {
      "type": "object",
      "description": "The set of properties specific to the Azure ML web service resource.",
      "required": [
        "packageType"
      ],
      "discriminator": "packageType",
.......
        "packageType": {
          "type": "string",
          "description": "Specifies the package type. Valid values are Graph (Specifies a web service published through the Machine Learning Studio) and Code (Specifies a web service published using code such as Python). Note: Code is not supported at this time.",
          "enum": [
            "Graph"
          ]        
        }
      }
    },
    "WebServicePropertiesForGraph": {
      "description": "Properties specific to a Graph based web service.",
      "x-ms-discriminator-value": "Graph",
      "allOf": [
        {
          "$ref": "#/definitions/WebServiceProperties"
        }
      ],

when deserializing an answer from the RestAPI, it fails:

Traceback (most recent call last):
  File "D:\VSProjects\msrest-for-python\msrest\serialization.py", line 837, in _instantiate_model
    response_obj = response(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'package_type'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "d:\vsprojects\azure-sdk-for-python\azure-mgmt\tests\test_mgmt_machinelearning.py", line 288, in test_ml
    webservice = async_webservice.result()
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 473, in result
    self.wait(timeout)
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 486, in wait
    raise self._exception
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 367, in _start
    self._poll(update_cmd)
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 463, in _poll
    self._response)
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 292, in get_status_from_resource
    self.resource = self._deserialize(response)
  File "D:\VSProjects\msrestazure-for-python\msrestazure\azure_operation.py", line 177, in _deserialize
    resource = self.get_outputs(response)
  File "d:\vsprojects\azure-sdk-for-python\azure-mgmt-machinelearning\azure\mgmt\machinelearning\webservices\operations\web_services_operations.py", line 117, in get_long_running_output
    deserialized = self._deserialize('WebService', response)
  File "D:\VSProjects\msrest-for-python\msrest\serialization.py", line 762, in __call__
    value = self.deserialize_data(raw_value, attr_type)
  File "D:\VSProjects\msrest-for-python\msrest\serialization.py", line 888, in deserialize_data
    return self(obj_type, data)
  File "D:\VSProjects\msrest-for-python\msrest\serialization.py", line 768, in __call__
    return self._instantiate_model(response, d_attrs)
  File "D:\VSProjects\msrest-for-python\msrest\serialization.py", line 844, in _instantiate_model
    raise DeserializationError(msg + str(err))
msrest.exceptions.DeserializationError: Unable to deserialize {'realtime_configuration': <azure.mgmt.machinelearning.webservices.models.realtime_configuration.RealtimeConfiguration object at 0x04B14590>, 'description': None, 'output': <azure.mgmt.machinelearning.webservices.models.service_input_output_specification.ServiceInputOutputSpecification object at 0x04B14530>, 'storage_account': <azure.mgmt.machinelearning.webservices.models.storage_account.StorageAccount object at 0x04B14550>, 'read_only': False, 'diagnostics': <azure.mgmt.machinelearning.webservices.models.diagnostics_configuration.DiagnosticsConfiguration object at 0x04B145B0>, 'parameters': {'trigonometric Function': 'Sin', 'output mode': 'ResultOnly', 'category': 'Trigonometric', 'column Set': '{"isFilter":true,"rules":[{"ruleType":"ColumnTypes","exclude":false,"columnTypes":["Numeric"],"columnKinds":["All"]}]}'}, 'commitment_plan': None, 'machine_learning_workspace': None, 'assets': {'asset1': <azure.mgmt.machinelearning.webservices.models.asset_item.AssetItem object at 0x04B14630>}, 'keys': None, 'expose_sample_data': True, 'input': <azure.mgmt.machinelearning.webservices.models.service_input_output_specification.ServiceInputOutputSpecification object at 0x04B14610>, 'title': None, 'example_request': <azure.mgmt.machinelearning.webservices.models.example_request.ExampleRequest object at 0x04B145F0>, 'package_type': 'Graph'} into model <class 'azure.mgmt.machinelearning.webservices.models.web_service_properties.WebServiceProperties'>. __init__() got an unexpected keyword argument 'package_type'

And looking at the code, package_type is not a parameter of WebServiceProperties indeed:

class WebServiceProperties(Model):

    _validation = {
        'created_on': {'readonly': True},
        'modified_on': {'readonly': True},
        'provisioning_state': {'readonly': True},
        'swagger_location': {'readonly': True},
        'package_type': {'required': True},
    }

    _attribute_map = {
...
        'parameters': {'key': 'parameters', 'type': '{str}'},
        'package_type': {'key': 'packageType', 'type': 'str'},
    }

    _subtype_map = {
        'package_type': {'Graph': 'WebServicePropertiesForGraph'}
    }

    def __init__(self, title=None, description=None, keys=None, read_only=None, expose_sample_data=None, realtime_configuration=None, diagnostics=None, storage_account=None, machine_learning_workspace=None, commitment_plan=None, input=None, output=None, example_request=None, assets=None, parameters=None):
...
        self.parameters = parameters
        self.package_type = None

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
annatischcommented, Jan 25, 2017

@lmazuel - yes my post was less about the actual issue - and more in general. In this case I think a _subtype_map is missing on the parent class - though I need to take a close look…

1reaction
fearthecowboycommented, Jan 25, 2017

Well, that’s where it gets kindof funky…

AutoRest doesn’t know, the Modeler marks model types with PolymorphicDiscriminator, and sets the SerializedName to the value that the deserializer in the client uses to determine what type to create a runtime.

In c# this magic is applied with such a light touch, that it was tricky for me to even see how it worked. Specifically, (in c#):

  • the modeler marks a composite type with the Polymorphic Discriminator
  • the modeler sets the serialized name
  • the generator puts an attribute on the generated class with SerializedName (and skips that if SerializedName == the class name! )
  • the generator inserts a couple lines into the client:
SerializationSettings.Converters.Add(new Microsoft.Rest.Serialization.PolymorphicSerializeJsonConverter<@(polymorphicType.Name)>("@(polymorphicType.PolymorphicDiscriminator)"));
DeserializationSettings.Converters.Add(new  Microsoft.Rest.Serialization.PolymorphicDeserializeJsonConverter<@(polymorphicType.Name)>("@(polymorphicType.PolymorphicDiscriminator)"));

and that’s IT … The magic is handled by the client runtime and JSON.NET which is so f’ing subtle that it’s creepy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to deserialize to object: type, KeyError: ' key: int; value
The deserialization error says that python cannot deserialize the response got to an object of type UserEntitlementsPostResponse .
Read more >
msrest.serialization — Azure SDK for Python 2.0.0 ... - NET
Currently contains 'type' with the msrest type and 'key' with the RestAPI ... _attribute_map[subtype_key], response ) if subtype_value: # Try to match base ......
Read more >
Python Rest APIs: 5 Important Commands, Key Modules, and ...
An HTTP Response is returned by a Python REST API or any API after it ... have to manually define the Content-Type and...
Read more >
Solve common issues with JSON in SQL Server
I want to create a JSON text result from a simple SQL query on a single table. FOR JSON PATH and FOR JSON...
Read more >
Python and REST APIs: Interacting With Web Services
There's always a chance that requests to your REST API could fail. It's a good idea to define what an error response will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found