Safeguard
Developer Tools•

What is pytm and can you write a threat model as code?

pytm is an OWASP Python framework: describe a system in code and get a data flow diagram, sequence diagram and report. Setup needs and where it fits.

Safeguard Security Team
3 min read

Yes. pytm is an open source Python framework that lets you describe a system as code, then generates a data flow diagram, a sequence diagram and a report of matching threats. It suits teams that want threat models in version control, reviewed in pull requests like any other file.

What pytm does

According to the project page, pytm aims to move threat modeling earlier in development and make it more automated and developer centric. You write a Python script that declares the model's parts, then run it.

Core building blocks include a model object, servers, datastores, actors, lambdas, boundaries, data flows and the data that moves. You connect elements with a data flow that names a source, a sink and a label, and set attributes such as protocol, destination port, the boundary an element sits in, and its controls.

What you get out

  • A data flow diagram, written as Graphviz output and rendered with the dot tool
  • A sequence diagram, written as PlantUML output
  • A report, filled from a template, which can be converted with pandoc
  • A JSON dump and a list of threats
  • A staleness check that shows how far the model has drifted from the code it describes

Threat rules come from a database of more than 100 entries, and you can supply your own file. The project warns that the rule conditions are evaluated with eval, so protect that file like code.

What you need installed

The page lists Linux or macOS, Python 3.11 or later and Graphviz. Sequence diagrams need Java and a PlantUML jar, and HTML reports need pandoc. Docker and Devbox options exist to avoid manual installs. The page does not state the license type on the content I could read, so check the repository before adopting it.

Code or whiteboard: a quick comparison

QuestionModel as codeWhiteboard or drawing tool
Reviewed in pull requestsYesOnly if exported
Easy for non-developersHarderEasier
Shows drift from codeHas a staleness checkManual
Setup effortHigherLower
Good forTeams that live in the repoMixed groups, first sessions

A sensible path is to hold the first session at a whiteboard, then encode the agreed diagram as code if the team wants it kept current.

Tips for keeping a code model honest

  1. Keep the model file in the same repository as the system it describes.
  2. Run the staleness check in CI so a drifting model is noticed.
  3. Treat generated threats as prompts for discussion, not as findings.
  4. Review model changes in the same pull request as design changes.

Quick check

Does pytm replace a threat modeling session?

No. It generates candidate threats from the structure you describe. The conversation about what matters and what to do remains a human job.

Is a model as code better than a drawing?

Neither is better in general. Code is easier to version and review. Drawings are easier for mixed groups. Pick what your team will actually keep up to date.

A note on tooling

pytm models the design. Safeguard checks what you build from it, with SBOM and dependency scanning and policy gates that can run on pull requests. A free scan takes a repository link.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.