Skip to main content

Documentation Index

Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

AI SDKs

Here are the configuration options for contexts in AI SDKs:

.NET AI

In the .NET AI SDK, you can construct a Context that only has a key by calling Context.New. The context kind defaults to “user,” or you can supply a different context kind. Alternatively, you can use the Context.Builder method for building a context with other properties.The argument to Builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.Here’s an example:
      LDContext context = Context.Builder("example-context-key")
          .Set("firstName", "Sandy")
          .Set("lastName", "Smith")
          .Set("email", "sandy@example.com")
          .Set("groups", LdValue.ArrayOf(LdValue.Of("Acme"), LdValue.Of("Global Health Services")))
          .Build();
Here’s how to construct a context with a context kind of something other than “user”:
      var context2 = Context.New(ContextKind.Of("organization"), "example-organization-key");
Here’s how to construct a multi-context, which includes multiple context kinds:
      var userContext = Context.New("example-context-key");

      var deviceContext = Context.Builder("example-device-key")
          .Kind("device")
          .Build();

      var multiContext = Context.NewMulti(userContext, deviceContext);

The optional name and kind attributes, which you can set with .Name() and .Kind(), expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. The SDK uses the LdValue type to represent arrays and objects. The .NET SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your AI Config targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your AI Config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI Config. To learn more, read Target with flags and Customizing AI Configs.To learn how to configure private attributes in the .NET AI SDK, read Private attributes.To learn how to configure anonymous contexts in the .NET AI SDK, read Anonymous contexts and users.

Go AI

The Go AI SDK defines a Context struct and a Builder. The context key is the only mandatory context attribute. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.Here’s an example:
      import (
          "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
          "github.com/launchdarkly/go-sdk-common/v3/ldvalue"
      )

      // Context with only a key
      // by default, the context kind is "user"
      context1 := ldcontext.New("example-context-key")

      // Context with a key plus other attributes
      context2 := ldcontext.NewBuilder("context-key-456def").
          Kind("organization").
          Name("Global Health Services").
          SetString("email", "info@globalhealthexample.com").
          SetValue("address", ldvalue.ObjectBuild().
              SetString("street", "123 Main Street").
              SetString("city", "Springfield")).
          SetValue("groups", ldvalue.ArrayOf(
            ldvalue.String("Acme"), ldvalue.String("Global Health Services"))).
          Build()
Here’s how to construct a context with a context kind of something other than “user”:
      context1 := ldcontext.NewWithKind("organization", "example-organization-key")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multiContext := ldcontext.NewMulti(
          ldcontext.New("example-user-key"),
          ldcontext.NewWithKind("device", "example-device-key")
      )
Each individual context within a multi-context can have the same attributes. The only restriction is that each context has to have a different context kind from the others within the multi-context.You can also use the context builder to create each of the individual contexts:
      multiContext := ldcontext.NewMulti(
          ldcontext.NewBuilder("example-user-key").Name("Sandy").Build(),
          ldcontext.NewBuilder("example-device-key").Kind("device").Name("iPad").Build(),
      )
The kind and name attributes expect string values. You can set the kind, or, if you do not set it, LaunchDarkly assumes that the context kind is “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects. These types are all represented by the ldvalue.Value type. The Go SDK is strongly-typed, so be aware of this distinction.
If an attribute is a JSON object, then in your AI Config targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your AI Config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI Config. To learn more, read Target with flags and Customizing AI Configs.To learn how to configure private attributes in the Go AI SDK, read Private attributes.To learn how to configure anonymous contexts in the Go AI SDK, read Anonymous contexts and users.

Node.js (server-side) AI

In the Node.js (server-side) AI SDK, contexts are JSON objects. The key property is the context key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.Here’s an example:
      const context: LDContext = {
        kind: 'user',
        key: 'example-user-key',
        firstName: 'Sandy',
        lastName: 'Smith',
        email: 'sandy@example.com',
        groups: ['Acme', 'Global Health Services'],
      };
Here’s how to construct a context with a context kind of something other than “user”:
      const context: LDContext = {
         kind: 'device',
         key: 'example-device-key'
      }
Here’s how to construct a multi-context, which includes multiple context kinds:
      const context: LDContext = {
        kind: 'multi',
        user: { key: 'example-user-key' },
        device: { key: 'example-device-key' }
      }
The optional name and kind attributes expect string values. If the kind attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or JSON objects.
If an attribute is a JSON object, then in your AI Config targeting, you can use / as a delimiter to refer to specific fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your AI Config message. Continuing the same example, you could use {{ldctx.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI Config. To learn more, read Target with AI Configs and Customizing AI Configs.To learn how to configure private attributes in the Node.js (server-side) AI SDK, read Private attributes.To learn how to configure anonymous contexts in the Node.js (server-side) AI SDK, read Anonymous contexts and users.

Python AI

In the Python AI SDK, the Context class has a create method for creating a context with a context kind of “user” and with only a key. It has a builder method for building a context with other properties.The argument to Context.builder is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key or a hash, as long as the same context always has the same key. We recommend using a hash if possible.Here’s an example:
      context = Context.builder("example-context-key") \
          .set("firstName", "Sandy") \
          .set("lastName", "Smith") \
          .set("email", "sandy@example.com") \
          .set("groups", ["Acme", "Global Health Services"]) \
          .build()
Here’s how to construct a context with a context kind of something other than “user”:
      context1 = Context.create("example-organization-key", "organization")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multi_context = Context.create_multi(
          Context.create("example-user-key"),
          Context.create("example-device-key", "device")
      )
If you have many attributes to set, you can also create a context from a dictionary:
      pre_existing_dict = {
          'key': 'example-context-key',
          'kind': 'user',
          'firstName': 'Sandy',
          'lastName': 'Smith',
          'email': 'sandy@example.com',
          'groups': ['Acme', 'Global Health Services'],
      }

      context = Context.from_dict(pre_existing_dict)
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user.” Other attribute values can be booleans, numbers, strings, arrays, or objects.
If an attribute is a JSON object, then in your AI Config targeting, you can use / as a delimiter to refer to specific fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting rules. You can use . as a delimiter in your AI Config message. Continuing the same example, you could use {{LDCTX.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI Config. To learn more, read Target with AI Configs and Customizing AI Configs.To learn how to configure private attributes in the Python AI SDK, read Private attributes.To learn how to configure anonymous contexts in the Python AI SDK, read Anonymous contexts and users.

Ruby AI

In the Ruby AI SDK, contexts are instances of LaunchDarkly::LDContext.The key property is the context’s key. The combination of key and kind must uniquely identify each context. For the key, you can use a primary key, an email address, or a hash string, as long as the same context always has the same key. We recommend using a hash string if possible.Here’s an example:
      context = LaunchDarkly::LDContext.create({
          key: "example-user-key",
          kind: "user",
          firstName: "Sandy",
          lastName: "Smith",
          email: "sandy@example.com",
          groups: ["Acme", "Global Health Services"]
      })
Here’s how to construct a context with a context kind of something other than “user”:
      context = LaunchDarkly::LDContext.with_key("example-context-key", "organization")
Here’s how to construct a multi-context, which includes multiple context kinds:
      multi_context = LaunchDarkly::LDContext.create_multi([
          LaunchDarkly::LDContext.with_key("example-user-key"),
          LaunchDarkly::LDContext.with_key("example-device-key", "device"),
      ])
The optional name and kind attributes expect string values. If the “kind” attribute is not specified, it is assumed to be “user” and the hash is assumed to be in the legacy user format. Other attribute values can be booleans, numbers, strings, arrays, or objects.
If an attribute is a JSON object, then in your flag or segment targeting, you can use / as a delimiter to refer to specific object fields. For example, if you have an “address” attribute that includes several fields, then you could use /address/city in your targeting. To learn more, read Target with flags.You can use . as a delimiter in your AI Config message. Continuing the same example, you could use {{LDCTX.address.city}} in your message, and the value of the “city” field will be substituted when you customize the AI Config. To learn more, read Target with AI Configs and Customizing AI Configs.To learn how to configure private attributes in the Ruby AI SDK, read Private attributes.To learn how to configure anonymous contexts in the Ruby AI SDK, read Anonymous contexts and users.