Skip to main content
The File Annotation deployment pack (dp:contextualization:cdf_file_annotation) annotates P&IDs, drawings, and other documents in Cognite Data Fusion (CDF) with Diagram Detect and pattern mode, and links them to the assets and files they mention. It deploys as a module for the Cognite Toolkit. A data workflow calls one function in Cognite Functions, fn_file_annotation, four times, once per stage: prepare, launch, finalize, and promote. A transformation then links annotated files to assets. Progress per file is tracked on the FileAnnotationState view, and results are written to CDF RAW for reporting.
File Annotation is a cherry-pickable module inside the Contextualization deployment pack (dp:contextualization). You can add it on its own or alongside the other contextualization modules.

Who it’s for

Data engineers deploy and tune the pipeline. Subject matter experts review suggested annotations. Use this pack when you want a configuration-driven pipeline that:
  • Annotates documents against assets and files in your data model.
  • Combines regular Diagram Detect with pattern-mode detection and automatic promotion of pattern hits.
  • Handles large volumes, multi-page files, site and unit scoping, and several instance spaces.
Use something else when:

What’s included

Prerequisites

  • The Cognite Toolkit version 0.8 or later is installed, and cdf.toml exists in your project root. See Setting up.
  • Authentication is configured with cdf auth init and cdf auth verify. See Authentication and authorization.
  • Files and assets are ingested, with aliases populated by the aliases update function from CDF Entity Matching. Diagram Detect matches on aliases.
  • You have an identity provider (IdP) application (IDP_CLIENT_ID, IDP_CLIENT_SECRET) and the source ID of an IdP group (GROUP_SOURCE_ID).
  • To upload the sample pattern catalog, the data plugin is enabled in cdf.toml:

Install and deploy

In config.<env>.yaml, set at minimum:
  • The file and target entity views and instance spaces. The defaults point to CogniteFile and CogniteAsset in sp_cdm_instances.
  • functionClientId, functionClientSecret, and groupSourceId.
  • ApplicationOwner, which the Streamlit apps require, and environment, which is used in the group name.
Then build and deploy:
1

Build deployment artifacts

The Toolkit substitutes template variables and writes artifacts to the build/ directory. If you need to change the project name or any variables marked <change_me>, edit config.<env>.yaml first — see Configure, build, and deploy modules.
2

Dry-run the deployment

Inspect the output and confirm that configurations look correct before deploying.
3

Deploy to CDF

The Toolkit deploys only configurations that have changed since the last run.
4

Set up CI/CD (optional)

For governed production deployments, automate build, dry-run, and deploy in a pipeline. See Set up CI/CD pipelines.
If you previously deployed separate prepare, launch, finalize, and promote functions, use the build output from that deployment to preview the cleanup:
The cdf clean command deletes every resource managed by the configurations in the build, not only the four legacy functions. Review the dry-run output and continue only if you can safely delete every listed resource. Then run cdf clean, rebuild, and deploy the current module. Custom callers must now pass stage to fn_file_annotation.
Review the dry-run output before you run cdf clean. The command deletes every resource managed by that build.
Optional: tag test data. If your instances aren’t tagged yet, run the tag helpers once. They add tags without overwriting existing ones, and they don’t set aliases:
Optional: upload the sample pattern catalog:

Verify the deployment

  1. cdf deploy finishes without errors.
  2. Tag at least one file ToAnnotate and its assets DetectInDiagrams.
  3. In Integrate > Data Workflows, run wf_ctx_file_annotation and confirm that all five tasks succeed.
  4. In Integrate > Staging (RAW), confirm that raw_file_annotation.annotation_documents_tags has rows.
  5. Open the annotated file in CDF Search and confirm the annotations are visible.

How it works

Large documents are processed in 50-page chunks. Finalize uses optimistic locking, so concurrent finalize tasks do not overwrite one another’s updates. You can add more finalize tasks to the workflow (same function, stage: finalize) to increase throughput.

Tags and aliases

Tags decide which instances the pipeline processes. They’re never matched against diagram text. Aliases are the strings that Diagram Detect and promote look for. To reprocess finished files, add Annotated or AnnotationFailed to filesToAnnotateTags. They’re then removed from the exclude list automatically.

One aliases function per project

Use one aliases function for the whole project: fn_dm_context_aliases_update from the CDF Entity Matching module. It writes aliases on time series, assets, and files, so entity matching and file annotation match on the same strings. Don’t run a second alias pipeline for file annotation. Two pipelines with different patterns write conflicting aliases, and matches then succeed in one module and fail in the other. The aliases update function joins tag parts with -, so the name VAL_23-KA-9101 gets the alias 23-KA-9101. Earlier versions wrote 23_KA_9101. Keep these settings coordinated when you change either module: Run the aliases update before the file annotation workflow whenever names change, so launch and promote see current aliases.

Configuration reference

Set these variables in config.<env>.yaml before running cdf build. Required variables must be set for your project. Optional variables can keep their defaults. Diagram Detect limits, batch sizes, and RAW table names are fixed in functions/fn_file_annotation/fa_constants.py. Edit that file and redeploy to change them.

Views and spaces

Matching

After changing thresholds, run once with cleanOldAnnotations: true so annotations from earlier runs are replaced.

Tags

Scope

Without scope, every file is matched against all tagged entities. The same tag text often exists on several sites, which causes false positives, and large projects can exceed the Diagram Detect limit of 500,000 entities per call. With scope set, launch batches files by their scope values and only includes entities with the same values, plus ScopeWideDetect entities in the same primary scope. The property must be a text property with the same name on the file view and the target entity view. The core CogniteFile and CogniteAsset views don’t have one, so use views that extend them. For a file with site=PlantA and unit=U100:
Files and assets without a primary scope value are still matched, against everything, and launch logs a warning. Set the scope values on every file and asset you annotate.

Multiple instance spaces

Leave fileInstanceSpace and targetEntityInstanceSpace empty ("") when each site keeps its files and assets in its own space. Prepare then picks up files from every space. Each file is matched only against entities in its own space, and annotations and state are stored next to the file. You can also set one and leave the other empty, for example one shared asset space with files in many spaces. With an empty space, the transformations and debugFileExternalId don’t work, because they need a single space. The access group needs read and write on every space you annotate.

Text normalization

Promote uses entityNormalizationPatterns (assets) and fileNormalizationPatterns (files) to extract tag forms from detected text before searching aliases. Use the same patterns as the alias patterns in the aliases update function, so promote looks for the forms your aliases contain. See One aliases function per project. When several patterns match, the longest form wins. An empty list turns normalization off for that source. Lookups are case-sensitive exact matches. In the current version, promote builds its search forms by joining capture groups with _, plus the detected text as written and a form with separators removed. It doesn’t generate the - form that the aliases update function now writes. A tag detected inside longer text, such as VAL_23-KA-9101, can therefore be rejected even though the alias 23-KA-9101 exists. Check annotation_documents_patterns for rejected tags that exist as assets.
A tag detected inside longer text can be rejected even when the matching alias exists. Promote doesn’t generate the - form that the aliases update function writes.
Write character classes such as [0-9], not \d. The Cognite Toolkit rejects backslash escapes in variables. A pattern without a capture group fails when the config loads.

Data workflow and access

The variables in this table belong in config.<env>.yaml, except logLevel, which is set on each workflow task.

Results and reporting

Results are written to the RAW database raw_file_annotation. To find tags that are on a drawing but not in your asset hierarchy, query annotation_documents_patterns for status = Rejected, or run the status report after promote. Text that Diagram Detect never associates with an entity or pattern isn’t stored.

Transformations

Troubleshooting

Check that the files are tagged ToAnnotate and don’t carry an exclude tag such as Annotated. To reprocess annotated files, add Annotated or AnnotationFailed to filesToAnnotateTags.
Launch found no candidates for the file’s scope. Tag assets and reference files DetectInDiagrams, populate aliases, and check that the scope values match. The pipeline tags the failed file AnnotationFailed, which prepare excludes. Add AnnotationFailed to filesToAnnotateTags. Prepare then removes that tag from the exclude list and creates a FileAnnotationState with status New. Run the workflow again.
Too many entities in one call (limit 500,000). Set the scope properties, or split the data into separate instance spaces.
The normalized text doesn’t match any alias exactly. Check that one aliases function writes the aliases for the project, and that entityNormalizationPatterns match its assetAliasPattern. If the aliases use - and the tag was detected inside longer text, see Text normalization.
Lower the assetAuto* or fileAuto* thresholds, then run once with cleanOldAnnotations: true.
Check the view and instance space variables, and rebuild with cdf build. These transformations need a single instance space, so they don’t work when the space variables are empty.
Verify functionClientId, functionClientSecret, and groupSourceId, and that the group has access to every instance space you annotate. See Access management.
For more detail, set logLevel to DEBUG on the workflow task. See Data workflow and access. For issues not covered here, contact Cognite support.

Further reading

Support

Last modified on October 13, 2026