{"library":"pycapnp","title":"pycapnp: Cap'n Proto Python Bindings","description":"pycapnp is a Python wrapper for the C++ implementation of the Cap'n Proto data interchange format and RPC system. It provides insanely fast serialization and deserialization, often outperforming Protocol Buffers. The library is actively maintained, with regular releases bringing performance improvements, new features, and compatibility updates.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":["pip install pycapnp"],"cli":null},"imports":["import capnp"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import capnp\nimport os\n\n# Define a Cap'n Proto schema dynamically for demonstration\n# In a real application, this would be loaded from a .capnp file\n# e.g., addressbook = capnp.load('addressbook.capnp')\n\nSCHEMA_PATH = 'addressbook.capnp'\n\naddressbook_schema_content = '''\n@0xd411d7353f406691;\n\nstruct Person {\n  id @0 :UInt32;\n  name @1 :Text;\n  email @2 :Text;\n  phones @3 :List(PhoneNumber);\n\n  struct PhoneNumber {\n    number @0 :Text;\n    type @1 :Type;\n    enum Type {\n      mobile @0;\n      home @1;\n      work @2;\n    }\n  }\n\n  employment @4 :union {\n    unemployed @5 :Void;\n    employer @6 :Text;\n    school @7 :Text;\n    selfEmployed @8 :Void;\n  }\n}\n\nstruct AddressBook {\n  people @0 :List(Person);\n}\n'''\n\n# Write the schema to a temporary file\nwith open(SCHEMA_PATH, 'w') as f:\n    f.write(addressbook_schema_content)\n\ntry:\n    # Load the Cap'n Proto schema\n    addressbook = capnp.load(SCHEMA_PATH)\n\n    # 1. Build a message\n    with capnp.alloc_builder() as msg_builder:\n        addresses = msg_builder.init_root(addressbook.AddressBook)\n        people = addresses.init('people', 2)\n\n        alice = people[0]\n        alice.id = 123\n        alice.name = 'Alice'\n        alice.email = 'alice@example.com'\n        alice_phones = alice.init('phones', 1)\n        alice_phones[0].number = '555-1212'\n        alice_phones[0].type = 'mobile'\n        alice.employment.employer = 'Google'\n\n        bob = people[1]\n        bob.id = 456\n        bob.name = 'Bob'\n        bob.email = 'bob@example.com'\n        bob_phones = bob.init('phones', 2)\n        bob_phones[0].number = '555-4567'\n        bob_phones[0].type = 'home'\n        bob_phones[1].number = '555-7654'\n        bob_phones[1].type = 'work'\n        bob.employment.unemployed = None\n\n        # Serialize the message to bytes\n        serialized_bytes = msg_builder.to_bytes_packed()\n        print(f\"Serialized message size: {len(serialized_bytes)} bytes\")\n\n        # 2. Read a message\n        # Using capnp.alloc_reader() as a context manager is important for memory safety\n        with capnp.alloc_reader(serialized_bytes) as msg_reader:\n            read_addresses = msg_reader.get_root(addressbook.AddressBook)\n\n            for person in read_addresses.people:\n                print(f\"\\nPerson ID: {person.id}\")\n                print(f\"Name: {person.name}\")\n                print(f\"Email: {person.email}\")\n\n                print(\"Phones:\")\n                for phone in person.phones:\n                    print(f\"  - {phone.number} ({phone.type})\")\n\n                which_employment = person.employment.which()\n                if which_employment == 'employer':\n                    print(f\"Employment: Employer - {person.employment.employer}\")\n                elif which_employment == 'unemployed':\n                    print(\"Employment: Unemployed\")\n                else:\n                    print(f\"Employment: {which_employment}\")\n\nfinally:\n    # Clean up the temporary schema file\n    if os.path.exists(SCHEMA_PATH):\n        os.remove(SCHEMA_PATH)\n","lang":"python","description":"This quickstart demonstrates how to define a Cap'n Proto schema (dynamically for brevity), build a message by initializing a root object and its fields, and then serialize and deserialize it. It highlights the use of `capnp.load()` for schemas, initializing structs and lists, and safely reading messages using `capnp.alloc_builder()` and `capnp.alloc_reader()` context managers.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-16","installed_version":"2.2.2","pypi_latest":"2.2.2","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":2,"avg_import_s":0.22,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.1,"mem_mb":4.4,"disk_size":"37.2M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2,"import_time_s":0.07,"mem_mb":4.4,"disk_size":"37M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.17,"mem_mb":5.3,"disk_size":"39.4M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.16,"mem_mb":5.3,"disk_size":"39M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.39,"mem_mb":8.5,"disk_size":"31.2M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.35,"mem_mb":8.5,"disk_size":"31M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.41,"mem_mb":9,"disk_size":"30.9M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.6,"import_time_s":0.35,"mem_mb":9,"disk_size":"31M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.09,"mem_mb":4.3,"disk_size":"36.6M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"pycapnp","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":3,"import_time_s":0.08,"mem_mb":4.3,"disk_size":"36M"}]}}