Language Models API
DeterministicStopError
Bases: ValueError
An empty completion a retry cannot fix. Subclasses ValueError so
existing handlers keep working.
Source code in synalinks/src/modules/language_models/language_model.py
LanguageModel
Bases: Module
A language model API wrapper.
A language model is a type of AI model designed to generate, and interpret human language. It is trained on large amounts of text data to learn patterns and structures in language. Language models can perform various tasks such as text generation, translation, summarization, and answering questions.
We support providers that implement constrained structured output like Azure, Ollama or Mistral. In addition we support providers that otherwise allow to constrain the use of a specific tool like Groq or Anthropic.
For the complete list of models, please refer to the providers documentation.
The provider prefixes accepted in model (the part before the /) are
returned by LanguageModel.supported_providers().
Using OpenAI models
import synalinks
import os
os.environ["OPENAI_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="openai/gpt-4o-mini",
)
Using Groq models
import synalinks
import os
os.environ["GROQ_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="groq/llama3-8b-8192",
)
Using Anthropic models
import synalinks
import os
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="anthropic/claude-3-sonnet-20240229",
)
Using Mistral models
import synalinks
import os
os.environ["MISTRAL_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="mistral/codestral-latest",
)
Using Ollama models
Using Azure models
import synalinks
import os
os.environ["AZURE_API_KEY"] = "your-api-key"
os.environ["AZURE_API_BASE"] = "your-api-base"
os.environ["AZURE_API_VERSION"] = "your-api-version"
language_model = synalinks.LanguageModel(
model="azure/<your_deployment_name>",
)
Using Google Gemini models
import synalinks
import os
os.environ["GEMINI_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="gemini/gemini-3.1-flash-lite-preview",
)
Using XAI models
import synalinks
import os
os.environ["XAI_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="xai/grok-code-fast-1",
)
Using Cohere models
import synalinks
import os
os.environ["COHERE_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="cohere/command-r-plus",
)
Using DeepSeek models
import synalinks
import os
os.environ["DEEPSEEK_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="deepseek/deepseek-chat",
)
Using Together AI models
import synalinks
import os
os.environ["TOGETHER_AI_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
)
Using OpenRouter models
import synalinks
import os
os.environ["OPENROUTER_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="openrouter/anthropic/claude-3-haiku",
)
Using AWS Bedrock models
import synalinks
import os
os.environ["AWS_ACCESS_KEY_ID"] = "your-access-key"
os.environ["AWS_SECRET_ACCESS_KEY"] = "your-secret-key"
os.environ["AWS_REGION_NAME"] = "us-east-1"
language_model = synalinks.LanguageModel(
model="bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
)
Using Doubleword models
Doubleword exposes an OpenAI-compatible API. The doubleword/
prefix is rewritten to openai/ internally and api_base is
defaulted to https://api.doubleword.ai/v1, so structured outputs
flow through the standard OpenAI path. Set OPENAI_API_KEY to your
Doubleword API key (or pass api_base explicitly to override).
import synalinks
import os
os.environ["OPENAI_API_KEY"] = "your-doubleword-api-key"
language_model = synalinks.LanguageModel(
model="doubleword/qwen-qwen3-5-397b-a17b-fp8-dottxt",
)
Using Mirai models
Mirai is an on-device inference engine for Apple silicon, similar to
Ollama but faster (brew install mirai). Start the server first with the
model registry id; it downloads the model if needed and serves an
OpenAI-compatible API on http://localhost:8000/v1:
The server hosts that one model and ignores the model field of
requests, so the name after mirai/ is informational. The mirai/
prefix is rewritten to hosted_vllm/ internally (same wire format, no
API key) and api_base is defaulted to the local server, so structured
outputs flow through the JSON schema path. Pass api_base explicitly
for another host or port (--host/--port on the server side).
Using SGLang models
SGLang serves an OpenAI-compatible API, on http://localhost:30000/v1 by
default:
The sglang/ prefix is rewritten to hosted_vllm/ internally (same wire
format) and api_base is defaulted to SGLANG_API_BASE, then to the local
server, so structured outputs flow through the JSON schema path. For a
server started with --api-key, set HOSTED_VLLM_API_KEY.
Using a ChatGPT subscription (no API key)
The codex/ prefix reaches OpenAI models through a ChatGPT subscription
login instead of an API key. It does not go through LiteLLM: use
synalinks.OAuthLanguageModel, which sends the requests through the
locally signed-in Codex CLI (see its documentation for the setup and
limitations).
To cascade models in case there is anything wrong with
the model provider (hence making your pipelines more robust).
Use the fallback argument like in this example:
import synalinks
import os
os.environ["GEMINI_API_KEY"] = "your-api-key"
os.environ["ANTHROPIC_API_KEY"] = "your-api-key"
language_model = synalinks.LanguageModel(
model="anthropic/claude-3-sonnet-20240229",
fallback=synalinks.LanguageModel(
model="gemini/gemini-3.1-flash-lite-preview",
)
)
Note: Obviously, use an .env file and .gitignore to avoid
putting your API keys in the code or a config file that can lead to
leackage when pushing it into repositories.
Controlling reasoning effort
Reasoning ("thinking") models can be steered with the reasoning_effort
parameter, passed either as a default in the constructor or per call. It
accepts:
"none"(the default): send nothing, leaving the model at its provider default reasoning behavior."disable": actively turn native reasoning off. This only has an effect for providers that reason by default, i.e. ollama's thinking models (qwen3,deepseek-r1, ...), where it maps to ollama'sthink=Falsetoggle (safely ignored by non-thinking ollama models), and the ChatGPT backend (codex/), where it sends the lowest effort every model accepts ("low": most of them cannot turn reasoning off). Opt-in providers (OpenAI, Anthropic, Gemini, ...) reason only when explicitly enabled, so there is nothing to send and this value is a no-op for them.- any other value (e.g.
"low","medium","high"): forwarded to the provider as the reasoning effort, but only when the model supports reasoning (otherwise it is silently dropped).
import synalinks
language_model = synalinks.LanguageModel(
model="openai/o3-mini",
reasoning_effort="medium",
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model to use. |
None
|
api_base
|
str
|
Optional. The endpoint to use. |
None
|
timeout
|
int
|
Optional. The timeout value in seconds (Default to 600). |
600
|
retry
|
int
|
Optional. The number of retry (default to 5). |
5
|
retry_max_wait
|
int
|
Optional. Max seconds to wait between retries when a
rate-limit |
60
|
fallback
|
LanguageModel
|
Optional. The language model to fallback if anything is wrong. |
None
|
caching
|
bool
|
Optional. Enable caching of LM calls (Default to False). |
False
|
cache_dir
|
str
|
Optional. Directory for a persistent on-disk response cache. When set, every successful response is saved as a JSON file keyed by the full request (model, messages, schema and generation parameters), and identical requests are answered from disk without calling the provider, including across runs and processes. Streamed responses are cached once the stream is fully consumed (an abandoned stream is not cached) and a cache hit on a streaming call is replayed as a stream. (Default to None, disabled). |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
hooks
|
list
|
Optional. Hooks to attach to this module's calls. |
None
|
**default_kwargs
|
object
|
Optional. Default generation parameters (e.g.
|
{}
|
Source code in synalinks/src/modules/language_models/language_model.py
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 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 | |
call(messages, schema=None, tools=None, tool_schemas=None, streaming=False, **kwargs)
async
Call method to generate a response using the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
dict
|
A formatted dict of chat messages. |
required |
schema
|
dict
|
The target JSON schema for structed output (optional). If None, output a ChatMessage-like answer. |
None
|
tools
|
list | dict
|
Optional iterable or |
None
|
tool_schemas
|
list
|
Optional iterable of already-wire-formatted
tool declaration dicts (OpenAI |
None
|
streaming
|
bool
|
Enable streaming (optional). Default to False. Can be enabled only if schema is None. |
False
|
**kwargs
|
keyword arguments
|
The additional keywords arguments forwarded to the LM call. |
{}
|
Returns: (dict): The generated structured response.
Source code in synalinks/src/modules/language_models/language_model.py
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 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 | |
model_info(model)
classmethod
Returns a model's info from litellm's model table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model, e.g. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in synalinks/src/modules/language_models/language_model.py
ollama_capabilities()
The capabilities an Ollama server reports for this model.
Returns:
| Type | Description |
|---|---|
list
|
E.g. |
|
server cannot be reached or does not know the model. |
Source code in synalinks/src/modules/language_models/language_model.py
provider_info(provider)
classmethod
Returns what a frontend needs to set a provider up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
A provider prefix from |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported. |
Source code in synalinks/src/modules/language_models/language_model.py
supported_models(provider, api_base=None)
async
classmethod
Returns the language models a provider offers.
Self-hosted providers and open catalogs (Ollama, vLLM, SGLang, Mirai,
OpenRouter, Doubleword) are asked for the models they serve, keeping the Ollama
models that can chat. The others are listed from litellm's model table;
for OpenAI with OPENAI_API_KEY set, narrowed to the models the account
can use (with its fine-tuned models), from OpenAI's live list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
A provider prefix from |
required |
api_base
|
str
|
Optional. The server to ask, for a provider listed
from its server or from OpenAI's live list (defaults to its base
URL variable, then to its default endpoint, see
|
None
|
Returns:
| Type | Description |
|---|---|
list
|
The sorted model IDs, ready to pass as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported, or no endpoint is known to list its models from. |
ConnectionError
|
If the provider's server cannot be reached. |
Source code in synalinks/src/modules/language_models/language_model.py
supported_providers()
classmethod
Returns the provider prefixes supported for structured output.
These are the values accepted before the / in model, e.g.
"openai" in "openai/gpt-4o-mini". Some are routed to a different
litellm provider internally (ollama to ollama_chat, vllm to
hosted_vllm, doubleword to openai, mirai and sglang to
hosted_vllm); the names listed here are the ones to write in model.
Other litellm providers can still be used for plain chat completion, but
structured output raises for them.
Returns:
| Type | Description |
|---|---|
list
|
The sorted list of supported provider prefixes. |
Source code in synalinks/src/modules/language_models/language_model.py
supports_audio()
Whether the model accepts audio in its input.
Looked up like supports_vision: an Ollama model reports audio
among its capabilities, any other model is looked up in litellm's
model table (litellm.register_model with supports_audio_input:
True makes an unlisted one pass).
import synalinks
lm = synalinks.LanguageModel(model="gemini/gemini-2.5-flash")
print(lm.supports_audio()) # True
Returns:
| Type | Description |
|---|---|
bool
|
True if the model can listen to audio. |
Source code in synalinks/src/modules/language_models/language_model.py
supports_reasoning()
Whether reasoning_effort is forwarded to the provider.
Looked up in litellm's model table: when True, the effort is sent
with the request (see also supports_reasoning_trace()).
Returns:
| Type | Description |
|---|---|
bool
|
True if the model takes a reasoning effort. |
Source code in synalinks/src/modules/language_models/language_model.py
supports_reasoning_trace()
Whether the native reasoning trace fills the thinking field.
When True and a reasoning effort is sent, the thinking field is left
out of the schema handed to the model and filled with the
reasoning_content of the response instead. When False, the model
writes thinking like any other field.
Returns:
| Type | Description |
|---|---|
bool
|
True if the reasoning trace replaces |
Source code in synalinks/src/modules/language_models/language_model.py
supports_softprompt()
Whether the provider accepts soft prompts (prompt embeddings).
Only vLLM takes embeddings in place of prompt tokens (its
prompt_embeds input, served with --enable-prompt-embeds); the
hosted APIs take text and media only.
Returns:
| Type | Description |
|---|---|
bool
|
True if the model is served by vLLM. |
Source code in synalinks/src/modules/language_models/language_model.py
supports_vision()
Whether the model accepts images in its input.
An Ollama model is asked for its capabilities (its server reports
vision for the multimodal ones); any other model is looked up in
litellm's model table, where an unlisted model counts as text-only.
Register a self-hosted vision model with litellm.register_model
(supports_vision: True) to make it pass.
import synalinks
lm = synalinks.LanguageModel(model="openai/gpt-4o-mini")
print(lm.supports_vision()) # True
Returns:
| Type | Description |
|---|---|
bool
|
True if the model can read images. |
Source code in synalinks/src/modules/language_models/language_model.py
StreamingIterator
Async iterator over LM stream chunks.
Wraps litellm's CustomStreamWrapper (which is async-iterable via
__aiter__/__anext__) and yields one normalized dict per
non-empty chunk: {"role": "assistant", "reasoning_content": ..., "content": ...}.
Chunks containing only role/finish markers are skipped so reasoning-only
deltas don't terminate the stream prematurely.
Also accepts a plain sync iterator, useful for tests that mock
litellm.acompletion.
When language_model and request_start are provided, the iterator times
each consumed stream and, once exhausted, records two latencies onto the
LM's operational counters (all-time + the phase captured at request time):
time-to-first-token (request start -> first non-empty chunk) and
time-to-last-token (request start -> final non-empty chunk). These drive
the AvgTimeToFirstToken / AvgTimeToLastToken metrics. A stream that the
caller abandons before exhaustion is not recorded (no terminal timing).
When trajectory_start is also provided (an agent set a trajectory_scope),
a third latency is recorded: whole-trajectory time-to-first-token, measured
from the outermost agent's start (so it includes every tool-calling round)
to the first token of the final answer. This drives
AvgTrajectoryTimeToFirstToken.
Source code in synalinks/src/modules/language_models/language_model.py
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 | |
aconsume(name=None)
async
Drain the stream to exhaustion and return a concrete assistant
ChatMessage JsonDataModel.
The result is the same shape a non-streamed schema-less LM call
returns (role assistant, joined content, joined reasoning_content
when present), so a drained stream is indistinguishable downstream from
an ordinary prediction -- which is what lets the batched predict /
evaluate loops stream and still score the output. Exhausting the stream
here also records the time-to-first / time-to-last token latencies.
Source code in synalinks/src/modules/language_models/language_model.py
current_call_usage()
Return the usage of the LanguageModel call that just ran in this task.
None before any call and for streamed calls (usage is only known once
the stream is consumed), {"cache_hit": True} when the response came from
the file cache, otherwise a dict with input_tokens, output_tokens,
total_tokens, cache_read_input_tokens, cache_creation_input_tokens,
reasoning_tokens, cost (None when the provider reports none),
elapsed_s and finish_reason. When every attempt failed, error (and
fallback when a fallback answered).
Source code in synalinks/src/modules/language_models/language_model.py
OAuthLanguageModel
Bases: LanguageModel
A language model reached through a subscription login instead of an API key.
Flat-rate subscriptions (ChatGPT Plus/Pro/Business) authenticate with
OAuth and come without an API key, so they cannot be used through
LiteLLM. This class is a drop-in replacement for LanguageModel that
sends each request through the vendor's own CLI, which holds the login.
Only the Codex CLI (codex/ prefix) is supported: it is the only one
that enforces a JSON schema server-side, which Synalinks relies on for
constrained structured output.
Install the CLI with the codex extra (or with
npm install -g @openai/codex) and sign in once (interactive, opens a
browser):
pip install 'synalinks[codex]'
"$(python -c 'import codex_cli_bin; print(codex_cli_bin.bundled_codex_path())')" login
The CLI of the extra is used when installed, else codex from the
PATH; without either, the first call raises an ImportError.
Then use it like any other language model:
import synalinks
language_model = synalinks.OAuthLanguageModel(
model="codex/gpt-6.1-sol",
reasoning_effort="low",
)
synalinks.language_models.get("codex/gpt-6.1-sol"), and therefore
language_model="codex/gpt-6.1-sol" on any module, returns this class too.
Use "codex/default" to let the CLI pick the default model of the
account, and codex debug models to list the available ones.
How it works
Only the transport differs from LanguageModel: message formatting,
cache, retry, fallback, usage counters and response parsing are
inherited. A call without tools spawns codex exec stripped down to a
plain completion endpoint (a call with tools spawns codex app-server,
see below):
- The output schema is passed with
--output-schemaand enforced by the backend in strict mode: every field is required and returned. - The system message replaces the built-in coding-agent prompt (a short default one is sent when there is none), and the harness adds nothing to the request: it holds the messages and the schema.
- The model gets no tool: the shell, web search, apps and plugins are
disabled, and the tools the model catalog of the CLI turns on for a
model (the "code mode" of the gpt-6 models, sub-agents, the patch
tool) are taken out of it. The catalog is read once with
codex debug models; if the CLI has none, a warning says so. - Whatever the model is given, the sandbox is read-only, without network, and the working directory is an empty temporary directory. A tool call the CLI reports fails the call.
- The subprocess only inherits an allowlist of environment variables, so the API keys of the parent process are not exposed to it.
Tool calling
tools and tool_schemas are accepted as with LanguageModel, so a
FunctionCallingAgent (and the agents built on it) runs unchanged on a
codex/ model. A turn that declares tools, or whose history holds tool
calls, goes through codex app-server (one process per completion too),
where tool calling is native:
- The tools are declared to the thread (
dynamicTools): the model sees them as the functions of the request, as on the API. - The earlier turns are appended to the thread's history as the
Responses API items they are (
thread/inject_items): user and assistant messages,function_call,function_call_output. The last user message starts the turn. - The model's first call comes back as an
item/tool/callrequest. The turn is interrupted there and the call returned as a native tool call, which the caller runs: Synalinks keeps the loop, the CLI runs nothing.tool_choice="none"declares no tool; a structured answer over a tool history uses the turn'soutputSchema. - The same lockdown applies: read-only sandbox without network, empty
working directory, no approval, ephemeral thread, tool-free catalog,
allowlisted environment. A thread item that is not a message, a
reasoning or a call to a declared tool fails the call. The one
function the harness declares on its own,
request_user_input, is taken out of a turn with tools: the gpt-6 models call it, the CLI refuses it, and each attempt costs an unreported request.
Limitations
- Without tools, the messages are not sent as a chat. The system and
developer messages become the instructions of the CLI, as one block;
the rest becomes a single prompt: a lone user message verbatim, a
longer conversation as a role-tagged transcript (
[USER],[ASSISTANT]). Earlier assistant turns are therefore not sent as assistant messages (a turn with tools sends them as items, see above), and what reaches the backend is shaped by the CLI, which can change between releases (codex debug prompt-inputshows it). Results are not directly comparable with the same program on an API-key provider. - No streaming:
streaming=Trueraises aValueError. - A turn with tools goes through
codex app-server, which has no--ignore-user-config: the user'sconfig.tomlis loaded, and what it could add to a request is overridden key by key (its MCP servers are disabled by name, itsdeveloper_instructions,notifyandmodel_provideroverridden, the effort set to the model's default of the catalog when the call sets none).codex/defaulttakes the model of that configuration there.dynamicToolsis experimental in the app-server protocol of codex-cli 0.164 (thread/inject_itemsis not). tool_choice="required"or a named tool have no native form: they are asked for in the instructions, and an answer without the call is retried.parallel_tool_callsis not controlled: the CLI sendsparallel_tool_calls=false, so a turn carries one call.- Text only: images and audio parts are dropped, including those a tool returns.
- No sampling control: the CLI exposes neither
temperaturenormax_tokens, they are ignored with a warning. Do not rely on this class where a deterministictemperature=0.0judge is required. - A free-form
dictfield cannot be expressed in strict mode and raises aValueError. - The cost is not tracked (flat-rate subscription); token usage is, and each call counts against the usage limits of the subscription. The backend also caps concurrent requests per account (about five in flight, measured with codex-cli 0.162); beyond that, requests are refused and retried.
- One process per call. The Codex Python SDK (
openai-codex, a persistentcodex app-server) sends the same request for the same settings and was not measurably faster. - OpenAI recommends API-key authentication for programmatic Codex workflows; check the terms of your subscription before unattended use.
reasoning_effort behaves as with LanguageModel: a value ("low",
"medium", "high"...) is sent to the backend, "none" and None
keep the default of the model. "disable" and "minimal" send "low",
the lowest effort every model accepts (most of them cannot turn
reasoning off). A thinking field of the output schema is written by
the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model to use, as |
None
|
api_base
|
str
|
Unused, accepted for compatibility with
|
required |
timeout
|
int
|
The timeout in seconds of one call, after which the subprocess is killed (Default to 600). |
required |
retry
|
int
|
The number of attempts per call (Default to 5). |
required |
retry_max_wait
|
int
|
The maximum wait in seconds between two attempts (Default to 60). |
required |
fallback
|
LanguageModel
|
The language model to use if every attempt failed (Optional). |
required |
caching
|
bool
|
Unused, accepted for compatibility with
|
required |
cache_dir
|
str
|
The directory of the persistent response cache (Optional). |
required |
name
|
str
|
The name of the language model (Optional). |
required |
description
|
str
|
The description of the language model (Optional). |
required |
hooks
|
list
|
The hooks of the language model (Optional). |
required |
**default_kwargs
|
keyword arguments
|
The default parameters of
every call (e.g. |
required |
Source code in synalinks/src/modules/language_models/oauth_language_model.py
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 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 | |
call(messages, schema=None, tools=None, tool_schemas=None, streaming=False, **kwargs)
async
Call method to generate a response using the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
dict
|
A formatted dict of chat messages. |
required |
schema
|
dict
|
The target JSON schema for structed output (optional). If None, output a ChatMessage-like answer. |
None
|
tools
|
list | dict
|
Optional |
None
|
tool_schemas
|
list
|
Optional wire-format tool declarations, as
with |
None
|
streaming
|
bool
|
Not supported, must be False. |
False
|
**kwargs
|
keyword arguments
|
The additional keywords arguments
forwarded to the LM call (e.g. |
{}
|
Returns: (dict): The generated structured response.
Source code in synalinks/src/modules/language_models/oauth_language_model.py
provider_info(provider)
classmethod
Returns what a frontend needs to set a provider up.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
A provider prefix from |
required |
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported. |
Source code in synalinks/src/modules/language_models/oauth_language_model.py
supported_models(provider, api_base=None)
async
classmethod
Returns the models the Codex CLI offers (those of its model picker).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
A provider prefix from |
required |
api_base
|
str
|
Unused, accepted for compatibility with
|
None
|
Returns:
| Type | Description |
|---|---|
list
|
The sorted model IDs, ready to pass as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported. |
ImportError
|
If the Codex CLI is not installed. |
ConnectionError
|
If the CLI prints no model catalog. |
Source code in synalinks/src/modules/language_models/oauth_language_model.py
supported_providers()
classmethod
Returns the provider prefixes this class accepts.
Returns:
| Type | Description |
|---|---|
list
|
The sorted list of supported provider prefixes. |
Source code in synalinks/src/modules/language_models/oauth_language_model.py
supports_audio()
supports_reasoning()
Whether the model takes a reasoning effort (always True).
The effort reaches the CLI as model_reasoning_effort.
supports_reasoning_trace()
Whether the reasoning trace fills the thinking field (always False).
The CLI only returns a one-line summary of the reasoning, and not on
every turn: the model writes the thinking field itself.