Skip to content

T-strings for Structured Data

CI PyPI - json-tstring PyPI - toml-tstring PyPI - yaml-tstring Python 3.14+ License: MIT

Parser-first JSON, TOML, and YAML backends for PEP 750 template strings.

T-strings (introduced in Python 3.14) give you f-string convenience with structured access to interpolation values via string.templatelib.Template. This project builds on that: write templates that look like the target format, and get validated text or parsed Python data back.

How it works

Templates are parsed into an AST first, interpolation values are validated and inserted into slots in the AST, then the AST is rendered to text or materialized to Python objects. This parse-first approach prevents structurally invalid output and injection by construction.

from yaml_tstring import render_data, render_text

name = "api"
replicas = 3
labels = {"app": "api", "team": "platform"}
env = {"LOG_LEVEL": "info", "WORKERS": "4"}
ports = [8080, 8443]

template = t"""\
service:
  name: {name}
  replicas: {replicas}
  labels: {labels}
  env: {env}
  ports: {ports}
"""

# render_data: t-string -> parsed Python data
data = render_data(template)
# {'service': {'name': 'api',
#              'replicas': 3,
#              'labels': {'app': 'api', 'team': 'platform'},
#              'env': {'LOG_LEVEL': 'info', 'WORKERS': '4'},
#              'ports': [8080, 8443]}}

# render_text: t-string -> valid YAML text
text = render_text(template)
# service:
#   name: "api"
#   replicas: 3
#   labels:
#     "app": "api"
#     "team": "platform"
#   env:
#     "LOG_LEVEL": "info"
#     "WORKERS": "4"
#   ports:
#     - 8080
#     - 8443

Packages

Pick the format you need:

Package Format Install
json-tstring JSON pip install json-tstring
toml-tstring TOML pip install toml-tstring
yaml-tstring YAML pip install yaml-tstring

tstring-core (shared runtime) and tstring-bindings (native extension) are pulled in automatically.

See also