Overview
Concrete tool for persisting and versioning Python objects and retrieving them by reference provided by the Wandb Weave library.
Description
weave.publish() saves any Python object (Model, Dataset, Prompt, Op, etc.) to the Weave backend with content-addressable versioning. If the object's content hash differs from the latest version, a new version is created. weave.ref() creates a reference to an existing object without retrieving it, and calling .get() on the reference materializes the object.
Usage
Use weave.publish() to save artifacts that should be versioned and retrievable. Use weave.ref() to create lightweight references for passing to other Weave APIs or for later retrieval.
Code Reference
Source Location
- Repository: wandb/weave
- File: weave/trace/api.py
- Lines: L118-178 (publish), L181-207 (ref)
Signature
def publish(obj: Any, name: str | None = None) -> ObjectRef:
"""Save and version a Python object.
Args:
obj: The object to save and version.
name: The name to save the object under.
Returns:
A Weave Ref to the saved object.
"""
def ref(location: str) -> ObjectRef:
"""Creates a Ref to an existing Weave object.
Args:
location: A Weave Ref URI, or "name:version" or "name".
Returns:
A Weave Ref to the object.
"""
Import
I/O Contract
Inputs (publish)
| Name |
Type |
Required |
Description
|
| obj |
Any |
Yes |
Python object to save (Model, Dataset, Prompt, etc.)
|
| name |
None |
No |
Name to save under (defaults to obj.name or class name)
|
Outputs (publish)
| Name |
Type |
Description
|
| return |
ObjectRef |
Immutable reference with entity, project, name, digest
|
Inputs (ref)
| Name |
Type |
Required |
Description
|
| location |
str |
Yes |
URI or short form ("name" or "name:version")
|
Outputs (ref)
| Name |
Type |
Description
|
| return |
ObjectRef |
Reference to the object (call .get() to materialize)
|
Usage Examples
Publish and Retrieve
import weave
weave.init("my-team/my-project")
# Publish a dataset
dataset = weave.Dataset(
name="my_dataset",
rows=[{"input": "hello", "expected": "world"}],
)
ref = weave.publish(dataset)
print(ref) # weave:///my-team/my-project/object/my_dataset:<digest>
# Retrieve the dataset
retrieved = weave.ref("my_dataset").get()
print(retrieved.rows[0])
Version-Specific Retrieval
import weave
# Get a specific version by digest
specific_ref = weave.ref("my_dataset:abc123")
dataset_v1 = specific_ref.get()
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.