Skip to content

Commit 32b7fb0

Browse files
docs(examples): robust version-id handling in build_voice_agent (id vs _id)
1 parent 999179d commit 32b7fb0

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

examples/build_voice_agent.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
from smallestai.atoms.helpers import as_page
2626

2727

28+
def _id_of(obj) -> str:
29+
"""Resource id — agents expose `_id`, versions expose `id`."""
30+
return getattr(obj, "id", None) or getattr(obj, "_id", None)
31+
32+
2833
def make_client() -> SmallestAI:
2934
api_key = os.environ["SMALLEST_API_KEY"]
3035
base = os.environ.get("SMALLEST_BASE_URL")
@@ -73,25 +78,25 @@ def main() -> None:
7378
print("5. draft a new version from the active one")
7479
versions = as_page(c.atoms.agent_versioning_versions.list_published_versions(id=agent_id))
7580
active = next((v for v in versions.items if getattr(v, "is_active", False)), None) or versions.items[0]
76-
src_version_id = getattr(active, "_id", None) or getattr(active, "id", None)
81+
src_version_id = _id_of(active)
7782
print(" source version:", src_version_id)
7883
draft = c.atoms.agent_versioning_drafts.create_a_draft(id=agent_id, source_version_id=src_version_id)
79-
draft_id = getattr(draft.data, "draft_id", None) or getattr(draft.data, "_id", None)
84+
draft_id = getattr(draft.data, "draft_id", None) or _id_of(draft.data)
8085
print(" draft id:", draft_id)
8186

8287
print("6. publish the draft with activate=True (go live)")
8388
published = c.atoms.agent_versioning_drafts.publish_a_draft(
8489
id=agent_id, draft_id=draft_id, label="v2-live", activate=True
8590
)
86-
new_version_id = getattr(published.data, "_id", None)
91+
new_version_id = _id_of(published.data)
8792
print(" published version:", new_version_id, "— waiting for it to go live...")
8893

8994
print("7. poll until the new version is active")
9095
live = False
9196
for i in range(10):
9297
time.sleep(4)
9398
vs = as_page(c.atoms.agent_versioning_versions.list_published_versions(id=agent_id))
94-
v = next((x for x in vs.items if getattr(x, "_id", None) == new_version_id), None)
99+
v = next((x for x in vs.items if _id_of(x) == new_version_id), None)
95100
sc = getattr(v, "security_check", None)
96101
status = getattr(sc, "status", None) if sc else None
97102
is_active = getattr(v, "is_active", None) if v else None

0 commit comments

Comments
 (0)