Skip to main content

pydantically-type-adapted-grpc framework for multimodal architectures team

Project description

mmar-ptag

pydantically-type-adapted-grpc framework for multimodal architectures team

The main goal: simplify defining type-safe Python interfaces to separate services.

Installation

pip install mmar-ptag

Quick Start

1. Define the Service

from types import SimpleNamespace
from mmar_ptag import deploy_server

class Greeter:
    def say_hello(self, *, name: str, count: int = 1) -> dict:
        return {"message": f"Hello, {name} x{count}"}

2. Run the Server

deploy_server(
    config_server=SimpleNamespace(port=50051, max_workers=10),
    service=Greeter()
)

3. Create a Client

from mmar_ptag import ptag_client

class Greeter:
    def say_hello(self, *, name: str, count: int = 1) -> dict:
        ...

client = ptag_client(Greeter, "localhost:50051")
result = client.say_hello(name="World", count=3)
print(result)  # {'message': 'Hello, World x3'}

Trace ID Support

Trace ID enables distributed tracing across your service mesh. Use installed_trace_id from mmar_mimpl to set the trace ID context.

from mmar_mimpl import installed_trace_id
from mmar_ptag import ptag_client

class UserService:
    def get_user(self, *, user_id: int) -> dict:
        ...

client = ptag_client(UserService, "localhost:50051")

# Set trace_id using installed_trace_id context manager
with installed_trace_id(trace_id="request-abc-123"):
    result = client.get_user(user_id=123)

The trace ID value is stored in mmar_mimpl.TRACE_ID_VAR, making it accessible throughout the request lifecycle and automatically propagated to downstream services.

How It Works

ptag uses Pydantic adapters to handle type conversion between Python and gRPC/protobuf.

Example: client.say_hello(name="World", count=3)

Client flow (sending request):

{name="World", count=3}
    -(args tuple)->
("World", 3)
    -(args_adapter.dump_json)->
["World", 3]
    -(wrap in PTAGRequest)->
b'\n\tsay_hello\x12\x0c["World", 3]'
    -(server receives)->

Server flow (processing & responding):

b'\n\tsay_hello\x12\x0c["World", 3]'
    -(args_adapter.validate_json)->
["World", 3]
    -(bind to kwargs)->
{name="World", count=3}
    -(say_hello method)->
{"message": "Hello, World x3"}
    -(result_adapter.dump_json)->
{"message": "Hello, World x3"}
    -(wrap in PTAGResponse)->
b'\n\tsay_hello\x12\x1e{"message": "Hello, World x3"}'
    -(client receives)->

Client flow (receiving response):

b'\n\tsay_hello\x12\x1e{"message": "Hello, World x3"}'
    -(return_adapter.validate_json)->
{"message": "Hello, World x3"}

This ensures:

  • Arguments are validated and serialized before sending
  • Return values are deserialized and validated after receiving
  • Type safety across the wire without manual protobuf definitions

Features

  • Type-safe RPC using Pydantic for validation
  • Automatic reconnection with configurable retry attempts
  • Built-in tracing with trace ID support
  • Interface-based design – define services as Python classes
  • Keyword-only arguments – explicit and readable API

API Reference

ptag_client(interface, address, reconnect_attempts=5)

Create a dynamic client for the given interface at the provided gRPC address.

  • interface: Type (class) defining the service interface
  • address: gRPC server address (e.g., "localhost:50051")
  • reconnect_attempts: Number of retry attempts on connection failure (default: 5)

ptag_attach(server, service_object)

Attach a service object to an existing gRPC server.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mmar_ptag-1.2.2.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mmar_ptag-1.2.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file mmar_ptag-1.2.2.tar.gz.

File metadata

  • Download URL: mmar_ptag-1.2.2.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.31

File hashes

Hashes for mmar_ptag-1.2.2.tar.gz
Algorithm Hash digest
SHA256 11503c79e4fabb35199025b9ac0198af1775a504b05e1dde2fc2ff1a56962325
MD5 dd6925f3559558c385fa3292d88c6dd9
BLAKE2b-256 3840f531e532c91fb9c1cba2a88358403e2f47c845fc532bf9d9582b1cfc057c

See more details on using hashes here.

File details

Details for the file mmar_ptag-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: mmar_ptag-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.31

File hashes

Hashes for mmar_ptag-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 38868777004530c1aa91e450497ae0c429e8f5fad737a965f3f7af716f5c8498
MD5 9f2b5fa2152ebfc5ace00ca74a0e7d40
BLAKE2b-256 4ed1649eb904b408dcb9c432eef3b3228447c9d056d1a19d4c405ab8b35ade77

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page