

# Traverse relationships

Use `TraverseAgentContext` to return the neighbors of a seed node, with the edges that connect them. Get the seed node ID from a search result.

The `traversalStrategy` is required. Set `direction` to `OUTGOING`, `INCOMING`, or `BOTH`, and `maxDepth` to 1, 2, or 3 hops. Directions follow the edge: a table’s `AWS::GLUE::HAS_COLUMN` edge points from the table to the column, so it is `OUTGOING` from the table and `INCOMING` from the column. To follow only some relationships, list them in `edgeTypes`.

Each result is a neighbor with its `nodeId`, `properties`, `depth` (1 means directly connected), and `pathEdges`, the edges from the seed to that neighbor in order. An edge has no ID of its own; its `edgeId` is the `from` and `to` node IDs, and its type is in `properties.edgeType`.

A traversal must finish within 10 seconds. If it doesn’t, the call fails with `InternalServerException` and returns no partial results. On an Agent context with many relationships, a deep traversal across every edge type is the most likely to run out of time, so keep `maxDepth` at 1 or 2 and list only the `edgeTypes` you need.

For the full request and response fields, see `TraverseAgentContext` in the * AWS Context API Reference*.

## To traverse relationships
<a name="to-traverse-relationships"></a>

Because the traversal strategy is a nested structure, pass the request as JSON. Replace `node-1234567890abcdef` with a `nodeId` from a search result.

```bash
aws context traverse-agent-context --cli-input-json '{
  "agentContextId": "ac-1234567890abcdef",
  "nodeId": "node-1234567890abcdef",
  "traversalStrategy": { "direction": "BOTH", "maxDepth": 1 }
}'
```

The following example makes the same call with the AWS SDK for Python (Boto3).

```python
import boto3

client = boto3.client("context", region_name="us-east-1")
response = client.traverse_agent_context(
    agentContextId="ac-1234567890abcdef",
    nodeId="node-1234567890abcdef",
    traversalStrategy={"direction": "BOTH", "maxDepth": 1},
)
for neighbor in response["items"]:
    print(neighbor["depth"], neighbor["nodeId"])
```