Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Struct sub-fields cannot be JSON or dict marshaled #535

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

Open
aabmass opened this issue Feb 4, 2025 · 1 comment
Open

Struct sub-fields cannot be JSON or dict marshaled #535

aabmass opened this issue Feb 4, 2025 · 1 comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@aabmass
Copy link

aabmass commented Feb 4, 2025

Environment details

  • Programming language: Python 3.13
  • OS: Linux
  • Language runtime version: 3.13
  • Package version: proto-plus==1.26.0

Steps to reproduce

There is no way to marshal just a subfield of a message into JSON or a dict. Here's an example

import proto
from google.protobuf import json_format, struct_pb2


# Taken from VertexAI GAPIC client
# https://github.com/googleapis/python-aiplatform/blob/58fbabdeeefd1ccf1a9d0c22eeb5606aeb9c2266/google/cloud/aiplatform_v1beta1/types/tool.py#L293
class FunctionCall(proto.Message):
    name: str = proto.Field(
        proto.STRING,
        number=1,
    )
    args: struct_pb2.Struct = proto.Field(
        proto.MESSAGE,
        number=2,
        message=struct_pb2.Struct,
    )


function_call = FunctionCall()
function_call.name = "foo"

args = struct_pb2.Struct()
args.update({"arg1": "value1", "nest": [{"foo": "bar", "baz": "bar"}]})

# Works fine on the Struct class
print(json_format.MessageToDict(args))

# Fails with AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'
function_call.args = args
print(json_format.MessageToDict(function_call.args))
Details

$ python repro.py
{'arg1': 'value1', 'nest': [{'foo': 'bar', 'baz': 'bar'}]}
Traceback (most recent call last):
  File "/usr/local/google/home/aaronabbott/repo/opentelemetry-python-contrib/instrumentation-genai/opentelemetry-instrumentation-vertexai/repro.py", line 30, in <module>
    print(json_format.MessageToDict(function_call.args))
          ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/google/home/aaronabbott/repo/opentelemetry-python-contrib/.venv/lib/python3.13/site-packages/google/protobuf/json_format.py", line 162, in MessageToDict
    return printer._MessageToJsonObject(message)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/usr/local/google/home/aaronabbott/repo/opentelemetry-python-contrib/.venv/lib/python3.13/site-packages/google/protobuf/json_format.py", line 203, in _MessageToJsonObject
    message_descriptor = message.DESCRIPTOR
                         ^^^^^^^^^^^^^^^^^^
AttributeError: 'MapComposite' object has no attribute 'DESCRIPTOR'

The issue is that the StructRule.to_python() returns a MapComposite instead of the google.protobuf.struct_pb2.Struct

return (
None if absent else maps.MapComposite(value.fields, marshal=self._marshal)
)

@aabmass aabmass added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Feb 4, 2025
@aabmass aabmass changed the title Struct fields cannot be JSON or dict marshaled Struct sub-fields cannot be JSON or dict marshaled Feb 4, 2025
@vchudnov-g
Copy link
Contributor

Thanks for reporting this issue! We will investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants