Clients¶
Client for Interactions with LLMs (Language Models) This module provides a client interface for interacting with various LLMs. It includes functionality for sending requests, receiving responses, and handling errors.
AbstractClient ¶
AbstractClient(preset: Optional[str] = None, tools: Optional[List[Union[str, AbstractTool]]] = None, use_tools: bool = False, debug: bool = True, tool_manager: Optional[ToolManager] = None, **kwargs)
Bases: EventEmitterMixin, ABC
Abstract base Class for LLM models.
Source code in packages/ai-parrot/src/parrot/clients/base.py
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 | |
client
property
writable
¶
Return the SDK client bound to the current event loop, or None.
The cache key is id(asyncio.get_running_loop()). Returns None
when called outside a running loop (e.g. in __init__ or sync code).
| RETURNS | DESCRIPTION |
|---|---|
Optional[Any]
|
The loop-local SDK client instance, or |
Optional[Any]
|
been created yet for the current loop. |
has_server_credentials
classmethod
¶
Whether the server itself holds a usable credential for this provider (any credential_env resolves).
Source code in packages/ai-parrot/src/parrot/clients/base.py
get_client
abstractmethod
async
¶
complete
async
¶
complete(prompt: str, *, model: Optional[str] = None, system_prompt: Optional[str] = None, max_tokens: Optional[int] = None, temperature: Optional[float] = None) -> str
Send a prompt, return the model's textual reply as a plain string.
Thin convenience wrapper around ask() for single-shot,
tool-less text generation. Use cases:
- Interop with components that expect
async complete(prompt) -> str(e.g.parrot_tools.scraping.PlanGenerator). - Quick prompts where you don't need the full
AIMessage.
Handles two things ask() does NOT:
- Auto-enters the async context manager if the client isn't
already initialized, so callers don't need
async with client:before callingcomplete(). If the client was already entered (e.g. inside a surroundingasync with), we reuse it and don't tear it down. - Extracts plain text from the response —
ask()returns anAIMessagepydantic model (provider-specific clients) or aMessageResponseTypedDict with a content-block list. Both shapes collapse to a single string here.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
User prompt.
TYPE:
|
model
|
Override the instance default model.
TYPE:
|
system_prompt
|
Optional system prompt.
TYPE:
|
max_tokens
|
Override default max tokens.
TYPE:
|
temperature
|
Override default sampling temperature.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The model's textual response. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the response has no extractable text. |
Source code in packages/ai-parrot/src/parrot/clients/base.py
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 | |
close
async
¶
Close all per-loop SDK clients.
Delegates to close_all(). Both close and close_all exist
so callers can be explicit about intent (spec §8 Q1).
Source code in packages/ai-parrot/src/parrot/clients/base.py
close_all
async
¶
Tear down every per-loop SDK client entry.
Safely handles dead / foreign loops: entries whose loop has been
garbage-collected or belongs to a different running context are dropped
without awaiting their close() coroutine.
After this call, _clients_by_loop and _locks_by_loop are empty.
Source code in packages/ai-parrot/src/parrot/clients/base.py
set_program ¶
set_tools ¶
Set complete list of tools, replacing existing.
get_tool ¶
Get a tool by name from ToolManager or legacy tools.
Source code in packages/ai-parrot/src/parrot/clients/base.py
register_tool ¶
register_tool(tool: Union[ToolDefinition, AbstractTool] = None, name: str = None, description: str = None, input_schema: Dict[str, Any] = None, function: Callable = None) -> None
Register a Python function as a tool for LLM to call.
Source code in packages/ai-parrot/src/parrot/clients/base.py
register_tools ¶
Register multiple tools at once.
register_python_tool ¶
Register Python REPL tool with a ClaudeAPIClient.
| PARAMETER | DESCRIPTION |
|---|---|
client
|
The ClaudeAPIClient instance
|
report_dir
|
Directory for saving reports
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PythonREPLTool
|
The PythonREPLTool instance |
Source code in packages/ai-parrot/src/parrot/clients/base.py
list_tools ¶
Get a list of all registered tool names.
Source code in packages/ai-parrot/src/parrot/clients/base.py
remove_tool ¶
Remove a tool by name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Tool name to remove
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if tool was removed, False if not found |
Source code in packages/ai-parrot/src/parrot/clients/base.py
clear_tools ¶
ask
abstractmethod
async
¶
ask(prompt: str, model: str, max_tokens: Optional[int] = None, temperature: float = 0.7, files: Optional[List[Union[str, Path]]] = None, system_prompt: Optional[str] = None, history: Optional[Sequence[HistoryMessage]] = None, structured_output: Union[type, StructuredOutputConfig, None] = None, tools: Optional[List[Dict[str, Any]]] = None, use_tools: Optional[bool] = None, deep_research: bool = False, background: bool = False, lazy_loading: bool = False) -> MessageResponse
Send a prompt to the model and return the response.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The input prompt for the model
TYPE:
|
model
|
The model to use
TYPE:
|
max_tokens
|
Maximum number of tokens in the response.
TYPE:
|
temperature
|
Sampling temperature for response generation
TYPE:
|
files
|
Optional files to include in the request
TYPE:
|
system_prompt
|
Optional system prompt to guide the model
TYPE:
|
history
|
Already-rendered conversation history from the owning bot (FEAT-524). The client only formats it for its provider — it never loads or persists history itself.
TYPE:
|
structured_output
|
Optional structured output configuration
TYPE:
|
tools
|
Optional tools to register for this call
TYPE:
|
use_tools
|
Whether to use tools
TYPE:
|
deep_research
|
If True, use deep research mode (provider-specific)
TYPE:
|
background
|
If True, execute research in background (async mode)
TYPE:
|
lazy_loading
|
If True, enabled dynamic tool searching
TYPE:
|
Source code in packages/ai-parrot/src/parrot/clients/base.py
ask_stream
abstractmethod
async
¶
ask_stream(prompt: str, model: str = None, max_tokens: Optional[int] = None, temperature: float = 0.7, files: Optional[List[Union[str, Path]]] = None, system_prompt: Optional[str] = None, history: Optional[Sequence[HistoryMessage]] = None, tools: Optional[List[Dict[str, Any]]] = None, deep_research: bool = False, agent_config: Optional[Dict[str, Any]] = None, lazy_loading: bool = False) -> AsyncIterator[Union[str, AIMessage]]
Stream the model's response.
Yields successive string chunks of the model response followed by a
single final :class:~parrot.models.responses.AIMessage carrying full
response metadata (token usage, stop reason, model, provider, turn_id,
etc.).
Implementors MUST yield at least one str chunk before the final
AIMessage. Consumers can detect the end-of-stream sentinel via
isinstance(chunk, AIMessage).
Source code in packages/ai-parrot/src/parrot/clients/base.py
resume
abstractmethod
async
¶
Resume a suspended model execution.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID
TYPE:
|
user_input
|
The user's input to inject as tool result
TYPE:
|
state
|
The suspended state containing messages and tool_call_id
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MessageResponse
|
The response from the LLM
TYPE:
|
Source code in packages/ai-parrot/src/parrot/clients/base.py
batch_ask
async
¶
invoke
abstractmethod
async
¶
invoke(prompt: str, *, output_type: Optional[type] = None, structured_output: Optional[StructuredOutputConfig] = None, model: Optional[str] = None, system_prompt: Optional[str] = None, max_tokens: Optional[int] = None, temperature: float = 0.0, use_tools: bool = False, tools: Optional[list] = None) -> InvokeResult
Lightweight stateless invocation — no retry, no history, no prompt builder.
Each concrete client implements this method using provider-native structured
output. Use this instead of ask() when you need fast, stateless structured
extraction without conversation history overhead.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The user prompt to send.
TYPE:
|
output_type
|
A Pydantic model or dataclass class to parse the response into.
Mutually exclusive with
TYPE:
|
structured_output
|
Full :class:
TYPE:
|
model
|
Override the model for this call. Falls back to an
explicitly selected
TYPE:
|
system_prompt
|
Override the system prompt. Falls back to
TYPE:
|
max_tokens
|
Maximum completion tokens.
TYPE:
|
temperature
|
Sampling temperature (default 0.0 for deterministic output).
TYPE:
|
use_tools
|
If
TYPE:
|
tools
|
Additional tool definitions to pass directly to the provider.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
InvokeResult
|
class: |
InvokeResult
|
and optional |
| RAISES | DESCRIPTION |
|---|---|
|
class: |
Source code in packages/ai-parrot/src/parrot/clients/base.py
StreamingRetryConfig ¶
StreamingRetryConfig(max_retries: int = 3, base_delay: float = 1.0, max_delay: float = 60.0, backoff_factor: float = 2.0, jitter: bool = True, auto_retry_on_max_tokens: bool = True, token_increase_factor: float = 1.5, retry_on_rate_limit: bool = True, retry_on_server_error: bool = True)
Configuration for streaming retry behavior.
Source code in packages/ai-parrot/src/parrot/clients/base.py
OpenAIBaseClient ¶
Bases: AbstractClient
OpenAI-compatible wire protocol; carries NO OpenAI-provider defaults.
Subclasses that speak the OpenAI chat-completions wire protocol under a
provider-specific label (Bedrock Mantle, OpenRouter, Moonshot, Nvidia,
LocalLLM/vLLM, and — in Phase 2 — Groq/Zai via their native SDKs) should
inherit from this class instead of :class:~parrot.clients.openai.OpenAIClient
directly, so they never inherit OpenAI-the-provider defaults (OpenAI-only
model ids, Responses-API routing, Sora, etc.).
Initialize the OpenAI-compatible wire client.
| PARAMETER | DESCRIPTION |
|---|---|
api_key
|
Bearer token for the target endpoint. Providers supply
their own environment-variable default in their own
TYPE:
|
base_url
|
Base URL of the OpenAI-compatible endpoint. Providers
supply their own default in their own
TYPE:
|
**kwargs
|
Forwarded to :class:
DEFAULT:
|
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
get_client
async
¶
Build the default OpenAI-SDK-shaped async client.
Lazily imports openai.AsyncOpenAI so the SDK is only required
when an OpenAI-compatible client is actually instantiated. Subclasses
that wrap a native SDK (Groq, Zai) override this hook.
| RETURNS | DESCRIPTION |
|---|---|
Any
|
An |
Any
|
|
| RAISES | DESCRIPTION |
|---|---|
ImportError
|
If the |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
ask
async
¶
ask(prompt: str, model: Any | None = None, max_tokens: int | None = None, temperature: float | None = None, files: list[str | Path] | None = None, system_prompt: str | None = None, history: Sequence[HistoryMessage] | None = None, structured_output: type | StructuredOutputConfig | None = None, tools: list[dict[str, Any]] | None = None, use_tools: bool | None = None, lazy_loading: bool = False) -> AIMessage
Ask the OpenAI-compatible endpoint a question with optional conversation memory.
Generic chat-completions implementation shared by every
OpenAIBaseClient subclass that speaks the plain OpenAI wire
protocol. Subclasses needing Responses-API routing, deep-research
dispatch, or other OpenAI-provider-only behavior override this
method (currently only :class:~parrot.clients.openai.OpenAIClient,
which reuses :meth:_chat_completion/:meth:_run_tool_call_loop
from this base while keeping its own richer ask()).
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The prompt to send to the model.
TYPE:
|
model
|
The model to use, or
TYPE:
|
max_tokens
|
Maximum tokens for the response.
TYPE:
|
temperature
|
Sampling temperature.
TYPE:
|
files
|
Files to upload before the call.
TYPE:
|
system_prompt
|
System prompt to prepend.
TYPE:
|
structured_output
|
Structured output definition (Pydantic model,
dataclass, or explicit
TYPE:
|
user_id
|
User ID for conversation memory.
|
session_id
|
Session ID for conversation memory.
|
tools
|
Tools to register for this call.
TYPE:
|
use_tools
|
Whether to use tools; defaults to
TYPE:
|
lazy_loading
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AIMessage
|
The response from the model. |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the resolved model requires Responses-API
routing (:meth: |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
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 | |
resume
async
¶
Resume a suspended model execution.
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
The session ID.
TYPE:
|
user_input
|
The user's input to inject as a tool result.
TYPE:
|
state
|
The suspended state containing messages and tool_call_id.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
AIMessage
|
The response from the model. |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
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 | |
batch_ask
async
¶
Process multiple ask() requests sequentially.
No native batch API exists for the OpenAI wire protocol; requests
are processed one at a time via :meth:ask.
| PARAMETER | DESCRIPTION |
|---|---|
requests
|
A list of kwargs dicts, each forwarded to :meth:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[AIMessage]
|
The list of :class: |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
ask_stream
async
¶
ask_stream(prompt: str, model: Any | None = None, max_tokens: int | None = None, temperature: float | None = None, files: list[str | Path] | None = None, system_prompt: str | None = None, history: Sequence[HistoryMessage] | None = None, tools: list[dict[str, Any]] | None = None, use_tools: bool = True, structured_output: type | StructuredOutputConfig | None = None, lazy_loading: bool = False, **kwargs) -> AsyncIterator[str | AIMessage]
Stream a response with tool-use support and conversation memory.
Generic chat-completions streaming implementation shared by every
OpenAIBaseClient subclass. Subclasses needing Responses-API
routing or other OpenAI-provider-only streaming behavior override
this method (currently only :class:~parrot.clients.openai.OpenAIClient).
Routes through :meth:_chat_completion with stream=True
(FEAT-438 G3 — the single completion funnel) instead of calling the
SDK directly, so a subclass's funnel override applies to streaming
too.
When the model requests tool calls during streaming, the tool-call
chunks are accumulated, the tools executed between rounds, and a new
streaming request is issued with the tool results — mirroring the
_run_tool_call_loop() used by :meth:ask.
Yields successive string chunks followed by a final
:class:~parrot.models.responses.AIMessage.
| PARAMETER | DESCRIPTION |
|---|---|
use_tools
|
Whether to include tool definitions and run the
streaming tool-call loop. Defaults to
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the resolved model requires Responses-API
routing and this class does not override |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
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 | |
invoke
async
¶
invoke(prompt: str, *, output_type: type | None = None, structured_output: StructuredOutputConfig | None = None, model: str | None = None, system_prompt: str | None = None, max_tokens: int | None = None, temperature: float = 0.0, use_tools: bool = False, tools: list | None = None) -> InvokeResult
Lightweight stateless invocation routed through the completion funnel.
Generic implementation shared by every OpenAIBaseClient
subclass. A single call is made through :meth:_chat_completion
(FEAT-438 G3 — previously this bypassed the funnel entirely and
called the SDK directly) — no conversation history, no prompt
builder; use_tools=True is passed to the funnel so it always
uses .create() (matching the pre-FEAT-438 behavior of never
using .parse() here).
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
User prompt.
TYPE:
|
output_type
|
Pydantic model or dataclass to parse the response into.
TYPE:
|
structured_output
|
Full
TYPE:
|
model
|
Model override. Falls back to an explicitly selected
TYPE:
|
system_prompt
|
System prompt override.
TYPE:
|
max_tokens
|
Maximum completion tokens.
TYPE:
|
temperature
|
Sampling temperature.
TYPE:
|
use_tools
|
Whether to inject registered tools.
TYPE:
|
tools
|
Additional tool definitions (unused — kept for interface
parity with :class:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
InvokeResult
|
class: |
| RAISES | DESCRIPTION |
|---|---|
InvokeError
|
On provider errors, or if the client is not initialized. |
Source code in packages/ai-parrot/src/parrot/clients/openai_base.py
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 1685 1686 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 | |