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"
sessions = Sessions(app_name=app_name)
# 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)
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 | Optional[str | list[str]] | Text input from the user. Can give a single string or list of strings. | None |
dtmf | Optional[str] | DTMF input from the user. | None |
event | Optional[str] | Name of a system event to trigger (e.g. 'WELCOME'). | None |
event_vars | Optional[Dict[str, Any]] | Key-value map of variables to inject alongside the event. | None |
blob | bytes | 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 | Optional[Dict[str, Any]] | Key-value state maps to inject for the session turn. | None |
tool_responses | Optional[List[Dict[str, Any]]] | Pre-computed tool run outputs if mocking tool execution. | None |
audio | bytes | Raw audio bytes to send as user input. | None |
audio_config | Optional[Dict[str, Any]] | Custom turn-specific audio configurations. | None |
input_audio_config | Optional[Dict[str, Any]] | Custom gRPC properties for input audio (defaults to 16kHz linear PCM). | None |
output_audio_config | Optional[Dict[str, Any]] | Custom gRPC properties for output audio (defaults to 16kHz linear PCM). | None |
deployment_id | Optional[str] | Overrides the default deployment ID setting for this turn run. | None |
historical_contexts | Optional[List[Dict[str, Any]] | str] | An existing conversation ID (string) or raw list of dictionaries to pre-set past history. | None |
turn_count | Optional[int] | 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
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 | |
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
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 | |
send_event ¶
Source code in src/cxas_scrapi/core/sessions.py
async_bidi_run_session ¶
make_text_request ¶
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