Workspace¶
deep_db_agents.workspace ¶
Materialization of large results to file, outside the agent's context.
The pattern: datasets too large to "read" are saved to file (Parquet or CSV) in a workspace area; only metadata, a preview, and a few statistics are returned to the agent. This decouples data volume from context volume.
MaterializedResult
dataclass
¶
MaterializedResult(path: str, fmt: str, row_count: int, columns: list[str], preview: list[dict[str, Any]], stats: dict[str, dict[str, float]], files_update: dict[str, Any] | None = None, truncated: bool = False)
Metadata about a result saved to file (never the full rows).
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Path of the file the result was written to. |
fmt |
str
|
Format the result was written in ( |
row_count |
int
|
Total number of rows written. |
columns |
list[str]
|
Ordered list of column names. |
preview |
list[dict[str, Any]]
|
The first few rows, as a list of |
stats |
dict[str, dict[str, float]]
|
Per-column numeric statistics ( |
files_update |
dict[str, Any] | None
|
State update returned by a |
truncated |
bool
|
|
to_summary ¶
to_summary() -> str
Build a compact textual summary to return to the agent.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A multi-line summary describing where the result was saved, its |
str
|
columns, numeric statistics (if any), and a preview of the first rows. |
|
str
|
When the result was truncated, a warning that the file is incomplete is |
|
str
|
included. |
Source code in src/deep_db_agents/workspace.py
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 | |
json_bytes ¶
json_bytes(obj: Any) -> int
Estimate the serialized JSON size, in bytes, of a document/record.
Used by the document dialects to bound how much of a lazy cursor they pull into memory before materialization: they stop consuming once the accumulated JSON size would exceed the budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
The object to size (typically a document/record dict). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The UTF-8 byte length of |
Source code in src/deep_db_agents/workspace.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
materialize_result ¶
materialize_result(columns: Sequence[str], rows: Iterable[Sequence[Any]], backend: BackendProtocol, *, fmt: str = 'csv', filename: str | None = None, preview_rows: int = 5, max_bytes: int | None = None) -> MaterializedResult
Save rows to file (bounded by max_bytes) and return only the metadata.
Rows are serialized incrementally and consumed lazily: only whole rows whose
cumulative size stays below max_bytes are written, and iteration stops before the
limit is exceeded. The written file is therefore never larger than max_bytes and,
for a lazy rows source, memory stays bounded too. When the budget stops the write
early, the returned result is flagged as truncated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Sequence[str]
|
Ordered column names. |
required |
rows
|
Iterable[Sequence[Any]]
|
Row data, each row aligned with |
required |
backend
|
BackendProtocol
|
The deepagents backend used to write the file (workspace filesystem). |
required |
fmt
|
str
|
Output format, either |
'csv'
|
filename
|
str | None
|
Name of the file to write; if |
None
|
preview_rows
|
int
|
Number of rows to include in the returned preview. |
5
|
max_bytes
|
int | None
|
Maximum size of the written file, in bytes; |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MaterializedResult |
MaterializedResult
|
Metadata about the saved file, including path, row count, |
MaterializedResult
|
columns, a preview, numeric statistics, and whether the write was truncated. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
OSError
|
If writing the file via |
ValueError
|
If |
Source code in src/deep_db_agents/workspace.py
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 | |
take_within_bytes ¶
take_within_bytes(items: Iterable[Any], size_of: Callable[[Any], int], max_bytes: int | None) -> tuple[list[Any], bool]
Consume items lazily, keeping only those that fit within max_bytes.
Iteration stops as soon as adding the next item would exceed the budget, so a lazy source (e.g. a database cursor) is not fully pulled into memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
Iterable[Any]
|
The items to consume (any iterable, possibly lazy). |
required |
size_of
|
Callable[[Any], int]
|
Callable returning the byte size of a single item. |
required |
max_bytes
|
int | None
|
Maximum cumulative byte size to keep, or |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
tuple[list[Any], bool]: The kept items and a flag that is |
bool
|
stopped early because the budget was reached (i.e. more items were available). |
Source code in src/deep_db_agents/workspace.py
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 | |
write_command ¶
write_command(message: str, tool_call_id: str | None, files_update: dict[str, Any] | None) -> Command
Wrap a tool's file-write result in a Command for the agent.
Returns a Command carrying the tool message plus, when a StateBackend produced
a files_update, the files state update the agent must apply. External backends
(Filesystem/Store) persist directly, so files_update is None and no files
key is added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Textual summary returned to the agent as the tool result. |
required |
tool_call_id
|
str | None
|
Identifier of the originating tool call (from |
required |
files_update
|
dict[str, Any] | None
|
Files to apply to the agent state, or |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Command |
Command
|
The state update wrapping the tool message and any file updates. |
Source code in src/deep_db_agents/workspace.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |