context¶
- class verta.runtime.context(validate: Optional[bool] = False)¶
Context manager for aggregating key-value pairs into a custom log entry.
This class should not be instantiated directly in your model code. For all models deployed in Verta, the
predict()
method is wrapped inside acontext
by default.For local testing, a single instance can be instantiated as in the provided example.
- Parameters:
validate (bool, default False) – If
True
, each individual call tolog()
will verify that the value provided is JSON serializable.- Raises:
RuntimeError – If this context manager instance is nested inside the context of an existing instance.
Examples
import json from verta import runtime with runtime.context(validate=True) as ctx: # validate values are JSON-serializable my_model.predict(x) # your model, with your calls to runtime.log() print(json.dumps(ctx.logs())) # Same output after exiting the context manager: print(json.dumps(ctx.logs()))
- logs() Dict[str, Any] ¶
Return the currently stored, thread-local logs from inside this context manager. If called after exiting the context manager, the final collection of logs captured at the time of exit is returned.
- Returns:
logs (Dict[str, Any]) – Dictionary of logs collected within this context manager.