PythonSynthesis module
PythonScript
Bases: Trainable
The python code to transform a JSON object into another JSON object.
The script is executed inside the Monty (https://github.com/pydantic/monty) sandboxed Python interpreter, which implements only a subset of Python. Scripts must observe the following constraints:
- The input JSON object is exposed as a dict named
inputs; the script must assign the output JSON object to a variable namedresultbefore it ends. - Only this subset of the standard library is importable:
sys,os,typing,asyncio,re,datetime,json,math,pathlib. Notably,time,random,itertools,collections,functoolsand the rest of the stdlib are not available. - No third-party libraries can be imported (e.g.
numpy,pandas,pydantic). classdefinitions andmatchstatements are not supported; use functions andif/elifchains instead.- The host filesystem, environment variables and network are not
reachable from the script.
os,sysandpathlibimport but their dangerous surface is pruned or gated:open(),os.system,os.listdir,os.environ,os.path,sys.argvandPath.read_textare all unavailable. asynciois also a stub: onlyasyncio.runandasyncio.gatherare exposed. There is noasyncio.sleep,wait_for,Future,create_taskorTaskGroup, and no time primitives of any kind (timeis not importable either).- Tools bound to the module are exposed as global async callables
under their tool name. They must be awaited inside an
async defand driven withasyncio.run(...). Every tool call returns a dict: a tool wrappingasync def f(x) -> intyields{"result": <value>}, a tool that already returns a dict yields that dict directly. For example, with a bound toolweb_search:
import asyncio
async def main():
hits = await web_search(query=inputs.get("q"))
# hits is a dict — index the field you need
return {"answer": hits["results"][0]["title"]}
result = asyncio.run(main())
Independent tool calls can be fanned out with asyncio.gather.
Calling a tool without await returns a coroutine object, not the
real value.
- Execution is bounded by the module's timeout and by Monty's memory
limits; long-running or allocation-heavy scripts will be aborted.
Source code in synalinks/src/modules/synthesis/python_synthesis.py
PythonSynthesis
Bases: Module
A code Python code transformation on JSON data.
The script runs inside the Monty <https://github.com/pydantic/monty>_
sandboxed Python interpreter: the host filesystem, environment and network
are unreachable from the script. Monty only supports a subset of Python
(no third-party libraries, limited standard library, no class or match
statements), so the generated script must stay within what Monty can
execute.
This module features a python code as trainable variable, allowing the optimizers to refine the code during the training loop based on iterative feedback and automatic selection of the best script.
This module works ONLY with advanced optimizers (NOT the
RandomFewShot optimizer).
The module executes the entire Python script and expects the result to be stored in a variable named 'result' at the end of execution.
Example:
import synalinks
import asyncio
default_python_script = \
"""
def transform(inputs):
# TODO implement the code to transform the input grid into the output grid
return {"output_grid": inputs.get("input_grid")}
result = transform(inputs)
"""
async def main():
inputs = synalinks.Input(
data_model=synalinks.datasets.arcagi.get_input_data_model(),
)
outputs = await synalinks.PythonSynthesis(
data_model=synalinks.datasets.arcagi.get_output_data_model()
python_script=default_python_script,
default_return_value={"output_grid": [[]]},
)(inputs)
program = synalinks.Program(
inputs=inputs,
outputs=outputs,
name="python_script_synthesis",
description="A program to solve ARCAGI with python code",
)
If you want to explore the future of neuro-symbolic self-evolving systems, contact us. While these systems are not "hard" to code thanks to Synalinks, they requires technical knowledge and a deep understanding of multiple AI paradigm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
The target JSON schema.
If not provided use the |
None
|
data_model
|
DataModel | SymbolicDataModel | JsonDataModel
|
The target data model for structured output. |
None
|
python_script
|
str
|
The default Python script. |
None
|
seed_scripts
|
list
|
Optional. A list of Python scripts to use as seed for the evolution. If not provided, create a seed from the default configuration. |
None
|
default_return_value
|
dict
|
Default return value. |
None
|
return_python_script
|
bool
|
Wether or not to return the python script for evaluation. (Default to False). |
False
|
timeout
|
int
|
Maximum execution time in seconds. (Default 5 seconds). |
5
|
tools
|
list
|
Optional. A list of Naming gotcha: each tool is registered inside the sandbox
under |
None
|
sandbox_type
|
type
|
Optional. The |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
trainable
|
bool
|
Whether the module's variables should be trainable. |
True
|
call also accepts an optional sandbox kwarg. If given, the
script executes inside the caller-supplied MontySandbox and any
state (variables, imports, function defs) persists across successive
calls — useful at training time when candidate scripts share
cached state. When omitted, a fresh sandbox is built per call
(independent execution, the normal runtime contract).
Source code in synalinks/src/modules/synthesis/python_synthesis.py
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
execute(inputs, python_script, sandbox=None)
async
Execute the Python script in the sandbox with a timeout.