Sessions¶
Sessions is how you actually talk to a CXAS agent. You can send text, events, DTMF digits, raw audio bytes, or multimodal blobs — and get back the agent's response in a structured object you can inspect or render.
The class supports two modalities via the Modality enum:
Modality.TEXT(default) — synchronous request/response over gRPC. Perfect for evals, notebooks, and scripted tests.Modality.AUDIO— asynchronous bidirectional streaming over a WebSocket (BidiRunSession). This is what you use for phone or voice integrations.
Sessions also has a handy parse_result() method that pretty-prints the full conversation trace — tool calls, transfers, custom payloads — in the terminal or as colorful HTML in a Jupyter notebook.
Quick Example¶
from cxas_scrapi import Sessions
app_name = "projects/my-project/locations/us/apps/my-app-id"
from cxas_scrapi.utils.rate_limiter import RateLimiter
# Optional: configure a rate limiter to prevent project API quota limits
limiter = RateLimiter(requests_per_minute=60.0)
sessions = Sessions(app_name=app_name, rate_limiter=limiter)
# Start a conversation
session_id = sessions.create_session_id()
# Text turn
response = sessions.run(
session_id=session_id,
text="I need help with my bill",
)
sessions.parse_result(response)
# Send an event (e.g., a welcome trigger)
sessions.run(
session_id=session_id,
event="WELCOME",
event_vars={"caller_id": "+14155551234"},
)
# Audio turn — convert text to speech automatically
audio_response = sessions.run(
session_id=session_id,
text="Tell me my account balance",
modality="audio",
)
Reference¶
Sessions ¶
Bases: Common
Initializes the Sessions client.
Source code in src/cxas_scrapi/core/sessions.py
create_session_id ¶
run ¶
run(session_id, text=None, dtmf=None, event=None, event_vars=None, blob=None, blob_mime_type='application/octet-stream', variables=None, tool_responses=None, audio=None, audio_config=None, input_audio_config=None, output_audio_config=None, deployment_id=None, historical_contexts=None, turn_count=None, modality=TEXT, use_tool_fakes=False, turn_num=None, capture_agent_audio=False, background_noise_file=None, burst_noise_files=None, voice_config=None)
Sends inputs to a Conversational Agents Session and returns the response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id | str | Unique UUID string or identifying string (e.g. 'test1') for the session. | required |
text | str | list[str] | None | Text input from the user. Can give a single string or list of strings. | None |
dtmf | str | None | DTMF input from the user. | None |
event | str | None | Name of a system event to trigger (e.g. 'WELCOME'). | None |
event_vars | dict[str, Any] | None | Key-value map of variables to inject alongside the event. | None |
blob | bytes | None | Raw binary content (image, pdf, etc.) for multimodal inputs. | None |
blob_mime_type | str | Mime type for the blob (defaults to 'application/octet-stream'). | 'application/octet-stream' |
variables | dict[str, Any] | None | Key-value state maps to inject for the session turn. | None |
tool_responses | list[dict[str, Any]] | None | Pre-computed tool run outputs if mocking tool execution. | None |
audio | bytes | None | Raw audio bytes to send as user input. | None |
audio_config | dict[str, Any] | None | Custom turn-specific audio configurations. | None |
input_audio_config | dict[str, Any] | None | Custom gRPC properties for input audio (defaults to 16kHz linear PCM). | None |
output_audio_config | dict[str, Any] | None | Custom gRPC properties for output audio (defaults to 16kHz linear PCM). | None |
deployment_id | str | None | Overrides the default deployment ID setting for this turn run. | None |
historical_contexts | list[dict[str, Any]] | str | None | An existing conversation ID (string) or raw list of dictionaries to pre-set past history. | None |
turn_count | int | None | Truncates historical context limits when pulling from a saved conversation ID. | None |
modality | Modality | str | Running via text (synced) or audio (asynchronous bidirectional streaming). Defaults to Modality.TEXT. | TEXT |
use_tool_fakes | bool | Use fake tools for the session if available. Defaults to False. | False |
Source code in src/cxas_scrapi/core/sessions.py
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 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 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 | |
parse_result ¶
Parses the CX Agent Studio session response to extract and print turn-by-turn interactions including User Queries, Agent Responses, Tool Calls, Tool Results, and Agent Transfers. Requires Jupyter Notebook or IPython environment for HTML rendering.
Source code in src/cxas_scrapi/core/sessions.py
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 | |
send_event ¶
Source code in src/cxas_scrapi/core/sessions.py
async_bidi_run_session ¶
async_bidi_run_session(config, inputs, turn_num=None, capture_agent_audio=False, background_noise_file=None, bg_noise_snr=15.0)
Source code in src/cxas_scrapi/core/sessions.py
make_text_request ¶
Source code in src/cxas_scrapi/core/sessions.py
get_file_data staticmethod ¶
Reads a local file, returns a blob dict.
Source code in src/cxas_scrapi/core/sessions.py
Modality ¶
Bases: str, Enum