Code Reference
Technical implementation of the semantido project.
Decorators
@semantic_table
A class decorator to enrich SQLAlchemy models with semantic metadata.
This metadata is used by the SQLAlchemySemanticBridge to build a semantic layer,
helping tools and LLMs understand the purpose, context, and filtering requirements
of the underlying database table.Decorator for adding semantic information to SQLAlchemy models
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
description
|
str
|
A human-readable explanation of what the table represents. |
required |
synonyms
|
Optional[list[str]]
|
Alternative names for the entity (e.g., ["client", "customer"]). |
None
|
sql_filters
|
Optional[list[str]]
|
A list of SQL fragments used for default filtering or row-level security. |
None
|
application_context
|
Optional[str]
|
The technical or functional scope within the app. |
None
|
business_context
|
Optional[str]
|
The business domain or logic this table belongs to. |
None
|
Examples:
@semantic_table(
description="User information and login profile",
synonyms=["user profile", "client"],
application_context="Registered users on the platform"
)
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
Source code in src/semantido/decorators/semantic_table.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Generators
Extracts and synchronizes semantic metadata from SQLAlchemy models into a unified layer.
SQLAlchemySemanticBridge
Bridge between SQLAlchemy models and the semantic layer.
This class serves as the extraction engine that inspects SQLAlchemy's internal
registry to generate a structured SemanticLayer. It handles the conversion of
SQL types to normalized types, builds join conditions for relationships, and
retrieves semantic metadata attached via decorators.
Source code in src/semantido/generators/semantic_bridge.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
get_semantic_layer()
Retrieves the current semantic layer instance.
Returns:
| Name | Type | Description |
|---|---|---|
SemanticLayer |
SemanticLayer
|
The object containing extracted table and relationship metadata. |
Source code in src/semantido/generators/semantic_bridge.py
55 56 57 58 59 60 61 62 | |
sync_from_models()
Extracts schema and semantic information from all mapped models.
This method clears any previously cached metadata and performs a full scan of the SQLAlchemy registry to rebuild the semantic layer.
Returns:
| Name | Type | Description |
|---|---|---|
SemanticLayer |
SemanticLayer
|
The fully populated semantic layer. |
Source code in src/semantido/generators/semantic_bridge.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
Defines the data structures for the semantic representation of the database schema.
Column
dataclass
Represents a database column with enriched semantic metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The physical name of the column in the database. |
data_type |
str
|
The normalized data type (e.g., VARCHAR, INTEGER). |
description |
str
|
A human-readable explanation of the column's content. |
privacy_level |
PrivacyLevel
|
The sensitivity classification of the data. |
sample_values |
Optional[list[str]]
|
A list of example data points to help clarify the content. |
synonyms |
Optional[list[str]]
|
Alternative terms for the column name. |
is_foreign_key |
bool
|
Boolean flag indicating if this column links to another table. |
references |
Optional[str]
|
The target table and column (format: 'table.column') if a foreign key. |
application_rules |
Optional[list[str]]
|
Specific business logic or constraints applied to this column. |
Source code in src/semantido/generators/semantic_layer.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
PrivacyLevel
Bases: Enum
Defines the data sensitivity levels for columns.
Used to inform downstream consumers (like LLMs or BI tools) about the accessibility and security requirements of specific data points.
Source code in src/semantido/generators/semantic_layer.py
23 24 25 26 27 28 29 30 31 32 33 | |
Relationship
dataclass
Represents a semantic link between two database tables.
Attributes:
| Name | Type | Description |
|---|---|---|
from_table |
str
|
The name of the source table. |
to_table |
str
|
The name of the target table. |
join_condition |
str
|
The SQL fragment defining how the tables link. |
relationship_type |
RelationshipType
|
The cardinality of the link. |
description |
str
|
A plain-language explanation of the relationship logic. |
Source code in src/semantido/generators/semantic_layer.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
RelationshipType
Bases: Enum
Specifies the cardinality of a database relationship.
Helps in determining how to construct joins and aggregate data.
Source code in src/semantido/generators/semantic_layer.py
36 37 38 39 40 41 42 43 44 45 | |
SemanticLayer
dataclass
The central repository for all semantic metadata extracted from the database.
This class serves as the final output of the synchronization process, containing structured information about tables, their relationships, and a global application glossary. It provides methods for serializing this metadata to JSON for use by external tools or LLMs.
Source code in src/semantido/generators/semantic_layer.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
add_relationship(relationship)
Registers a relationship between two tables in the semantic layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relationship
|
Relationship
|
The Relationship object defining the join logic and cardinality. |
required |
Source code in src/semantido/generators/semantic_layer.py
153 154 155 156 157 158 159 160 | |
add_table(table)
Registers a new table definition in the semantic layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
The Table object containing columns and semantic metadata. |
required |
Source code in src/semantido/generators/semantic_layer.py
144 145 146 147 148 149 150 151 | |
save_to_file(file_path)
Serializes and saves the semantic layer to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
The filesystem path where the JSON file will be created. |
required |
Source code in src/semantido/generators/semantic_layer.py
225 226 227 228 229 230 231 232 233 | |
to_dict()
Converts the entire semantic layer into a nested dictionary structure.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary representation suitable for JSON serialization. |
Source code in src/semantido/generators/semantic_layer.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
to_json()
Exports the entire semantic layer as a formatted JSON string.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Indented JSON string representing the semantic layer. |
Source code in src/semantido/generators/semantic_layer.py
216 217 218 219 220 221 222 223 | |
Table
dataclass
Represents a database table enriched with semantic and contextual information.
By capturing application and business contexts, this class helps disambiguate entities that might have generic names but specific roles in different domains.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The physical name of the table in the database. |
description |
str
|
A human-readable explanation of the table's purpose. |
columns |
list[Column]
|
A list of Column objects belonging to this table. |
primary_key |
str
|
The name of the primary key column. |
synonyms |
Optional[list[str]]
|
Alternative names for the entity represented by the table. |
sql_filters |
Optional[list[str]]
|
Default SQL fragments for filtering or security. |
application_context |
Optional[str]
|
The functional area of the application using this table. |
business_context |
Optional[str]
|
The business domain or logic this table serves. |
Source code in src/semantido/generators/semantic_layer.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
Models
Defines the standard and semantic-enabled SQLAlchemy declarative base classes.
Base
Bases: DeclarativeBase
Default SQLAlchemy base class for declarative models.
Source code in src/semantido/models/declarative_base.py
22 23 24 | |
SemanticDeclarativeBase
Bases: SemanticBase, DeclarativeBase
Mixin for declarative models with semantic data support.
Source code in src/semantido/models/declarative_base.py
27 28 | |