Context / Client / agentic_retrieve_stream

agentic_retrieve_stream

Context.Client.agentic_retrieve_stream(**kwargs)

Answers a natural-language question against an agent context and streams the work as it happens. The response is an event stream that contains progress events while AWS Context searches the agent context, the answer in chunks as it is generated, and a final result. The final result contains the answer and the ranked retrieved context.

See also: AWS API Documentation

Request Syntax

response = client.agentic_retrieve_stream(
    agentContextId='string',
    messages=[
        {
            'role': 'USER'|'ASSISTANT',
            'content': 'string'
        },
    ],
    sessionId='string',
    analysisConfiguration={
        'emitTrace': 'ENABLED'|'DISABLED'
    },
    retrievalConfiguration={
        'maxIterations': 123
    }
)
Parameters:
  • agentContextId (string) –

    [REQUIRED]

    The agent context to retrieve from, by its ID or ARN.

  • messages (list) –

    [REQUIRED]

    The conversation, as an ordered list of turns. A single-question caller sends one user message. The list carries multi-turn history so a follow-up resolves against earlier turns.

    • (dict) –

      A single conversation turn.

      • role (string) – [REQUIRED]

        The role of the participant that produced this turn. Valid values:

        • USER - The user asked or said this turn.

        • ASSISTANT - The assistant answered in this turn.

      • content (string) – [REQUIRED]

        The turn’s text content.

  • sessionId (string) – Optional. A key that you supply to group related calls, for example the turns of one conversation, for trace storage and analysis. Grouping is per account, across agent contexts, so use a value that is unique to each conversation.

  • analysisConfiguration (dict) –

    Optional controls for post-call analysis of the retrieval: whether its trace is stored for analysis. Trace events are streamed either way.

    • emitTrace (string) –

      Specifies whether AWS Context stores this call’s agent trace for asynchronous analysis. Absent means ENABLED. A trace is stored only when the agent context’s traceAnalysisConfig also has status and includeContextAgentTraces ENABLED.

  • retrievalConfiguration (dict) –

    Optional bounds and configuration for the agent loop.

    • maxIterations (integer) –

      The maximum number of agent turns, from 2 to 50. One turn is roughly one round of tool calls and their results. If you omit it, AWS Context uses 30. The minimum is 2, because the agent needs at least one planning turn and one acting turn.

Return type:

dict

Returns:

The response of this operation contains an EventStream member. When iterated the EventStream will yield events based on the structure below, where only one of the top level keys will be present for any given event.

Response Syntax

{
    'stream': EventStream({
        'traceEvent': {
            'id': 'string',
            'occurredAt': datetime(2015, 1, 1),
            'attributes': {
                'step': 'Planning'|'Retrieval',
                'status': 'IN_PROGRESS'|'SUCCEEDED'|'FAILED',
                'message': 'string',
                'actions': [
                    {
                        'inputQuery': 'string',
                        'sourceRetrievers': [
                            {
                                'identifier': 'string'
                            },
                        ]
                    },
                ],
                'warnings': [
                    {
                        'message': 'string'
                    },
                ],
                'failures': [
                    {
                        'message': 'string'
                    },
                ]
            }
        },
        'responseEvent': {
            'text': 'string'
        },
        'result': {
            'retrievedContext': [
                {
                    'nodeId': 'string',
                    'edgeId': {
                        'from': 'string',
                        'to': 'string'
                    },
                    'documentId': 'string',
                    'entityType': 'NODE'|'EDGE'|'DOCUMENT',
                    'summary': 'string',
                    'content': {
                        'byteContent': b'bytes',
                        'text': 'string',
                        'mimeType': 'string'
                    },
                    'properties': {
                        'string': {
                            'stringValue': 'string',
                            'intValue': 123,
                            'longValue': 123,
                            'boolValue': True|False,
                            'floatValue': ...,
                            'doubleValue': 123.0,
                            'stringListValue': [
                                'string',
                            ],
                            'intListValue': [
                                123,
                            ],
                            'longListValue': [
                                123,
                            ],
                            'floatListValue': [
                                ...,
                            ],
                            'doubleListValue': [
                                123.0,
                            ],
                            'boolListValue': [
                                True|False,
                            ]
                        }
                    }
                },
            ],
            'answer': 'string',
            'citations': [
                {
                    'startIndex': 123,
                    'endIndex': 123,
                    'references': [
                        {
                            'nodeId': 'string',
                            'edgeId': {
                                'from': 'string',
                                'to': 'string'
                            },
                            'documentId': 'string'
                        },
                    ]
                },
            ]
        },
        'accessDeniedException': {
            'message': 'string'
        },
        'resourceNotFoundException': {
            'message': 'string',
            'resourceId': 'string',
            'resourceType': 'agent-context'|'connection'|'curation-policy'|'evaluation'|'ontology'|'ontology-induction'|'resource-policy'
        },
        'internalServerException': {
            'message': 'string'
        }
    })
}

Response Structure

  • (dict) –

    • stream (EventStream) –

      The events of the retrieval, in order: progress events while the agent context is searched, chunks of the answer as it is written, and a final result with the answer and the retrieved context. An error event ends the stream.

      • traceEvent (dict) –

        A progress event: a planning or retrieval step started, succeeded, or failed.

        • id (string) –

          Unique identifier for the event.

        • occurredAt (datetime) –

          When the event occurred.

        • attributes (dict) –

          The step that the event reports on, the status of the step, and what happened in it.

          • step (string) –

            The step of the agent loop that this event reports on. Valid values:

            • Planning - The agent is planning which retrievals to run.

            • Retrieval - The agent is running a retrieval.

          • status (string) –

            The status of the step. Valid values:

            • IN_PROGRESS - The step started.

            • SUCCEEDED - The step finished successfully.

            • FAILED - The step failed. failures describes why.

          • message (string) –

            Human-readable description of the step’s state.

          • actions (list) –

            The retrievals the agent planned. Present on Planning events only.

            • (dict) –

              A retrieval the agent decided to run.

              • inputQuery (string) –

                The query the agent composed.

              • sourceRetrievers (list) –

                The sources that the retrieval targets: an agent context, a knowledge base, or a data source.

                • (dict) –

                  A source a retrieval targets.

                  • identifier (string) –

                    The source’s identifier: an agent context ARN, a knowledge base ID, or a data source ARN.

          • warnings (list) –

            Problems that did not stop the step.

            • (dict) –

              A problem that did not stop the step.

              • message (string) –

                A description of the problem. The step continued despite it.

          • failures (list) –

            Problems that failed the step.

            • (dict) –

              A problem that failed the step.

              • message (string) –

                A description of the problem that caused the step to fail.

      • responseEvent (dict) –

        A chunk of the generated answer. Concatenate the chunks, in the order received, to get the complete answer.

        • text (string) –

          The next chunk of the generated answer. Concatenate the text of every response event, in the order received, to get the complete answer, which the final result event also returns.

      • result (dict) –

        The final event of a successful stream: the complete answer and the context retrieved for it.

        • retrievedContext (list) –

          The ranked, structured context that the agent assembled across all iterations, without duplicates. Always present. Each entry is a graph node, a graph edge, or a document.

          • (dict) –

            One entity in the context that AgenticRetrieveStream returns: a graph node, a graph edge, or a document.

            • nodeId (string) –

              Identifier of the node. Set when this result is a node (one of nodeId, edgeId, or documentId).

            • edgeId (dict) –

              Identifier of the edge. Set when this result is an edge (one of nodeId, edgeId, or documentId).

              • from (string) –

                The ID of the node where the edge starts.

              • to (string) –

                The ID of the node where the edge ends.

            • documentId (string) –

              Identifier of the document. Set when this result is a document (one of nodeId, edgeId, or documentId).

            • entityType (string) –

              Specifies whether this result is a node, an edge, or a document. Valid values:

              • NODE - The result is a node.

              • EDGE - The result is an edge.

              • DOCUMENT - The result is a document.

            • summary (string) –

              Human-readable description or summary of the entity.

            • content (dict) –

              The retrieved content of the entity.

              • byteContent (bytes) –

                The binary content of the retrieved entity.

              • text (string) –

                The text content of the retrieved entity.

              • mimeType (string) –

                The Multipurpose Internet Mail Extensions (MIME) type of the retrieved content.

            • properties (dict) –

              Arbitrary key-value properties stored on the entity.

              • (string) –

                • (dict) –

                  The value of a property on a node or an edge in an agent context. Exactly one member is set, and the member that is set determines the value’s type.

                  Note

                  This is a Tagged Union structure. Only one of the following top level keys will be set: stringValue, intValue, longValue, boolValue, floatValue, doubleValue, stringListValue, intListValue, longListValue, floatListValue, doubleListValue, boolListValue. If a client receives an unknown member it will set SDK_UNKNOWN_MEMBER as the top level key, which maps to the name or tag of the unknown member. The structure of SDK_UNKNOWN_MEMBER is as follows:

                  'SDK_UNKNOWN_MEMBER': {'name': 'UnknownMemberName'}
                  
                  • stringValue (string) –

                    A string value, up to 2,048 characters.

                  • intValue (integer) –

                    A 32-bit signed integer value.

                  • longValue (integer) –

                    A 64-bit signed integer value.

                  • boolValue (boolean) –

                    A Boolean value.

                  • floatValue (float) –

                    A single-precision (32-bit) floating-point value.

                  • doubleValue (float) –

                    A double-precision (64-bit) floating-point value.

                  • stringListValue (list) –

                    A list of up to 1,000 string values, each up to 2,048 characters.

                    • (string) –

                  • intListValue (list) –

                    A list of up to 1,000 32-bit signed integer values.

                    • (integer) –

                  • longListValue (list) –

                    A list of up to 1,000 64-bit signed integer values.

                    • (integer) –

                  • floatListValue (list) –

                    A list of up to 1,000 single-precision (32-bit) floating-point values.

                    • (float) –

                  • doubleListValue (list) –

                    A list of up to 1,000 double-precision (64-bit) floating-point values.

                    • (float) –

                  • boolListValue (list) –

                    A list of up to 1,000 Boolean values.

                    • (boolean) –

        • answer (string) –

          The synthesized natural-language answer, when one was generated.

        • citations (list) –

          Maps spans of answer to the supporting entities, when an answer was generated.

          • (dict) –

            Maps a span of the answer to the entities that support it. The span is answer[startIndex:endIndex].

            • startIndex (integer) –

              The character offset in answer where the cited span begins.

            • endIndex (integer) –

              The character offset in answer where the cited span ends (exclusive).

            • references (list) –

              The entities in retrievedContext that support the span, referenced by identity.

              • (dict) –

                A reference by entity identity into retrievedContext (one of nodeId, edgeId, or documentId).

                • nodeId (string) –

                  Identifier of the referenced node. Set when the reference is a node.

                • edgeId (dict) –

                  Identifier of the referenced edge. Set when the reference is an edge.

                  • from (string) –

                    The ID of the node where the edge starts.

                  • to (string) –

                    The ID of the node where the edge ends.

                • documentId (string) –

                  Identifier of the referenced document. Set when the reference is a document.

      • accessDeniedException (dict) –

        You don’t have sufficient access to perform this operation. The stream ends after this event.

        • message (string) –

          A message that describes the error.

      • resourceNotFoundException (dict) –

        A resource that the retrieval needs was not found. The stream ends after this event.

        • message (string) –

          A message that describes the error.

        • resourceId (string) –

          The ID of the resource that was not found.

        • resourceType (string) –

          The type of the resource that wasn’t found.

          Valid values:

          • agent-context - An agent context.

          • connection - A connection.

          • curation-policy - A curation policy.

          • evaluation - Reserved for future use.

          • ontology - An ontology.

          • ontology-induction - An ontology induction.

          • resource-policy - A resource-based policy.

      • internalServerException (dict) –

        The retrieval failed because of an internal error. The stream ends after this event.

        • message (string) –

          A message that describes the error.

Exceptions