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
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 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 | |
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
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 | |
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