docs/src/content/docs/programming_guide/target_state.mdx
A target state represents what you want to exist in an external system. You declare target states in your code; CocoIndex keeps them in sync with your intent — creating, updating, or removing them as needed.
:::note[Terminology]
A target is the external system you write to — a directory, a database table, a vector store collection, etc. In Python, targets are represented by objects like DirTarget and TableTarget. A target state is what you want to exist in that target — a specific file, row, or embedding.
CocoIndex treats your declarations as the source of truth: if you stop declaring a target state, CocoIndex will remove it from the target. :::
Examples of target states:
When your source data changes, CocoIndex compares the newly declared target states with those from the previous run and applies only the necessary changes.
CocoIndex connectors provide targets with declare_* methods:
# Declare a file target state
dir_target.declare_file(filename="output.html", content=html)
# Declare a row target state
table_target.declare_row(row=DocEmbedding(...))
Target states can be nested — a directory contains files, a table contains rows. The container itself is a target state you declare, and once it's ready, you get a target to declare child target states within it.
Container target states (like a directory or table) are typically top-level — you can declare them directly. Child target states (like files or rows) require the container to be ready first.
Connectors provide convenience methods that mount the container and return a ready-to-use target in one step:
For simple cases where each processing component writes a single file, you can declare the file directly:
from cocoindex.connectors import localfs
# Declare a single file target state directly
localfs.declare_file(outdir / "output.html", html, create_parent_dirs=True)
When you need a DirTarget to declare multiple files, use the connector's convenience method:
# Mount a directory target, get a ready-to-use DirTarget
dir_target = await localfs.mount_dir_target(outdir)
# Declare child target states (files)
dir_target.declare_file(filename="output.html", content=html)
This example uses a ContextKey to reference the database connection — see Context for how keys are defined and provided.
import asyncpg
import cocoindex as coco
from cocoindex.connectors import postgres
# Define a ContextKey for the database connection (provided in lifespan)
TARGET_DB = coco.ContextKey[asyncpg.Pool]("target_db")
# Mount a table target, get a ready-to-use TableTarget
table = await postgres.mount_table_target(
TARGET_DB,
table_name="doc_embeddings",
table_schema=await postgres.TableSchema.from_class(
DocEmbedding, primary_key=["id"]
),
)
# Declare a child target state (a row)
table.declare_row(row=DocEmbedding(...))
These convenience methods wrap mount_target(), which automatically derives the component path from the target's globally unique key. See Processing Component for more on mounting APIs.
:::tip[Type safety]
Targets like DirTarget and TableTarget have two statuses: pending (just created) and resolved (after the container target state is ready). The type system tracks this — if you try to use a pending target before it's resolved, type checkers like mypy will flag the error.
:::
Under the hood, CocoIndex compares your declared target states with the previous run and applies the minimal changes needed:
<table> <thead> <tr> <th rowspan="2">Target State</th> <th colspan="3" style={{textAlign: 'center'}}>CocoIndex's Action</th> </tr> <tr> <th>On first declaration</th> <th>When declared differently</th> <th>When no longer declared</th> </tr> </thead> <tbody> <tr> <td>A database table</td> <td>Create the table</td> <td>Alter the table</td> <td>Drop the table</td> </tr> <tr> <td>A row in a database table</td> <td>Insert the row</td> <td>Update the row</td> <td>Delete the row</td> </tr> <tr> <td>A file in a directory</td> <td>Create the file</td> <td>Update the file</td> <td>Delete the file</td> </tr> </tbody> </table>CocoIndex ensures containers exist before their contents are added, and properly cleans up contents when the container changes.
When you change a container target state's declaration (e.g., add a column to a table schema, change a primary key), CocoIndex detects the change and does its best to alter the target in place. If the change is too large to alter (e.g., changing primary keys), the target is dropped and recreated.
When a target is dropped and recreated, CocoIndex automatically reprocesses all affected components to backfill the data — you don't need to manually trigger --full-reprocess. This is handled by the target connector's child invalidation mechanism, which signals to CocoIndex whether the change is destructive (all children lost) or lossy (some data may be lost).
For cases where connector-specific APIs don't cover your needs, CocoIndex provides generic APIs:
declare_target_state() — declare a leaf target statedeclare_target_state_with_child() — declare a target state that provides child target statesThese are exported from cocoindex and used internally by connectors. For defining custom targets, see Custom Target States Connector.