CodeModeAgent module
CodeModeAgent
Bases: Module
A code-mode agent that reasons by writing and executing Python.
Instead of emitting JSON tool calls, the language model writes a Python
snippet each turn. The snippet runs in a persistent
Monty <https://github.com/pydantic/monty>_ REPL sandbox, variables,
imports and function definitions accumulate across turns, so the agent
can build up intermediate values, probe data and iterate.
Bound tools (if any) appear inside the sandbox as global async
callables; scripts must await them inside an async def and drive
with asyncio.run(...). See CodeStep.python_code description for
the full Monty constraint list (supported stdlib, no classes, etc.).
Termination: the LM calls the always-present submit tool with the
final payload. If max_iterations is reached without submit,
a final inference step formats the accumulated trajectory into the
target schema / data_model. Empty python_code snippets are
no longer treated as a graceful exit, the loop feeds back a reminder
and keeps going.
Example:
import synalinks
import asyncio
class Query(synalinks.DataModel):
query: str
class Answer(synalinks.DataModel):
answer: str
async def web_search(query: str) -> list:
"""Search the web.
Args:
query (str): the search query.
"""
... # real implementation
async def main():
language_model = synalinks.LanguageModel(model="ollama/mistral")
inputs = synalinks.Input(data_model=Query)
outputs = await synalinks.CodeModeAgent(
data_model=Answer,
language_model=language_model,
tools=[synalinks.Tool(web_search)],
max_iterations=5,
)(inputs)
agent = synalinks.Program(inputs=inputs, outputs=outputs)
result = await agent(Query(query="Who discovered penicillin?"))
print(result.prettify_json())
if __name__ == "__main__":
asyncio.run(main())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict
|
Optional. The target JSON schema for the final
structured answer. If not provided, use |
None
|
data_model
|
DataModel | SymbolicDataModel | JsonDataModel
|
Optional. The target data model for the final answer. |
None
|
language_model
|
LanguageModel
|
The language model driving per-turn code generation and the final answer formatting. |
None
|
tools
|
list
|
Optional. A list of Naming gotcha: each tool is registered under
|
None
|
prompt_template
|
str
|
Optional. Prompt template forwarded to the per-turn code generator. |
None
|
examples
|
list
|
Optional. Examples forwarded to the per-turn code generator. |
None
|
instructions
|
str
|
Optional. Instructions for the per-turn code
generator. Defaults to |
None
|
final_instructions
|
str
|
Optional. Instructions for the final
answer generator. Defaults to |
None
|
temperature
|
float
|
Optional. Sampling temperature (Default 0.0). |
0.0
|
use_inputs_schema
|
bool
|
Optional. Feed the input schema to the generator prompt (Default False). |
False
|
use_outputs_schema
|
bool
|
Optional. Feed the output schema to the generator prompt (Default False). |
False
|
reasoning_effort
|
str
|
Optional. One of 'minimal', 'low', 'medium', 'high', 'disable', 'none', None. Default None. |
None
|
use_chain_of_thought
|
bool
|
Optional. Wrap the per-turn generator
in ChainOfThought so it emits a |
False
|
autonomous
|
bool
|
Optional. If True (default), run the full
code/execute/observe loop until the LM emits empty |
True
|
timeout
|
int
|
Per-turn execution budget in seconds (Default 5). Each snippet must finish within this budget; exceeding it turns into an observation so the LM can recover on the next turn. |
5
|
max_iterations
|
int
|
Maximum number of code-execution turns before forcing the final answer step (Default 5). |
5
|
max_output_chars
|
int
|
Maximum characters to include from REPL
output in the per-turn observation (Default 10_000). Anything
beyond is truncated with a |
10000
|
return_inputs_with_trajectory
|
bool
|
Optional. Whether to return the full trajectory alongside the final answer (Default True). |
True
|
sandbox_type
|
type
|
Optional. The |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
Source code in synalinks/src/modules/agents/code_mode_agent.py
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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 | |
CodeStep
Bases: DataModel
One turn of code-mode reasoning: a Python snippet to execute next.
Source code in synalinks/src/modules/agents/code_mode_agent.py
InputsSummary
Bases: DataModel
Metadata-only view of the user input bound as inputs in the sandbox.
Only per-field previews and sizes are surfaced here to keep the prompt
small when the input contains long documents or large collections. Read
the full values through inputs[field_name] inside your code —
the sandbox namespace holds the untruncated data.
Source code in synalinks/src/modules/agents/code_mode_agent.py
IterationInfo
Bases: DataModel
Budget info visible to the code generator on each turn.
Source code in synalinks/src/modules/agents/code_mode_agent.py
ToolSpec
Bases: DataModel
Description of one tool exposed in the code-mode sandbox.
Source code in synalinks/src/modules/agents/code_mode_agent.py
ToolsCatalog
Bases: DataModel
Catalog of tools bound to the code-mode sandbox.
Source code in synalinks/src/modules/agents/code_mode_agent.py
get_default_instructions()
The default code-mode agent instructions.