Python · recipe
Decode custom structures in Python
Ask the client to load the server's type definitions before reading a structured value. With them loaded, asyncua returns a Python object with named fields; without them you get an undecoded ExtensionObject and a pile of bytes.
| Stack | asyncua (FreeOpcUa/opcua-asyncio) |
|---|---|
| Version | 2.0.1 |
The code
import asyncio
from asyncua import Client
async def main():
async with Client(url="opc.tcp://opcua.umati.app:4840") as client:
# pull the server's DataTypeDefinitions and build Python classes
await client.load_data_type_definitions()
node = client.get_node("ns=4;i=7001") # a structured value
value = await node.read_value()
print(type(value).__name__)
for field, val in vars(value).items():
print(f" {field} = {val}")
asyncio.run(main())
What it prints
ProductionJobType
Identifier = JOB-2026-0912
State = 2
PartsPlanned = 500
PartsProduced = 431
Notes
- `load_data_type_definitions()` reads the server's DataTypeDefinition attributes and generates classes at runtime. Call it once after connecting, before reading any structure.
- If it returns nothing useful, the server may only publish a legacy binary type dictionary. That mismatch is a real interoperability gap, not a client bug.
- Unions and optional fields are where decoding usually breaks. Test those specifically rather than assuming the happy path generalises.
- For a one-off inspection, UaExpert shows decoded structures without any code.
What breaks first
The value comes back as raw bytes in an ExtensionObject instead of a typed object
Call load_data_type_definitions() after connecting. If the server offers only a legacy type dictionary, you need a client that reads dictionaries or a server-side change.
BadDataEncodingUnsupportedCall load_data_type_definitions() after connecting. If the server offers only a legacy type dictionary, you need a client that reads dictionaries or a server-side change.
Other recipes in this stack
Concepts on this page
Last verified 7 September 2026 against OPC UA 1.05.07. OPC UA Connect is an educational resource and is not affiliated with the OPC Foundation.