ory/kratos · error
ClientContextKey was expected to be *client.OryKratos but…
Error message
ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T
What it means
NewClient in cmd/cliclient/client.go looks up a client object stored in the command context under ClientContextKey. The value found must be *client.OryKratos; if the context key holds some other type (the %T in the message identifies it), the library refuses to continue because it cannot safely return a Kratos API client.
Solutions
- Check the %T in the message to see what type is actually stored under ClientContextKey.
- Store the value as *client.OryKratos (via kratos.NewAPIClient(kratos.NewConfiguration())) before invoking the commands.
- Use a separate context key per client library if you embed multiple Ory CLIs.
- Fix test stubs to implement/return the exact *client.OryKratos type.
Example fix
// before ctx = context.WithValue(ctx, client.ClientContextKey, myKetoClient) // after ctx = context.WithValue(ctx, client.ClientContextKey, kratos.NewAPIClient(kratos.NewConfiguration()))
Defensive patterns
Strategy: type-guard
Validate before calling
v := cmd.Context().Value(client.ClientContextKey)
if v != nil {
if _, ok := v.(*client.OryKratos); !ok {
return fmt.Errorf("context client has wrong type %T", v)
}
} Type guard
func kratosFromContext(ctx context.Context) (*client.OryKratos, bool) {
c, ok := ctx.Value(client.ClientContextKey).(*client.OryKratos)
return c, ok
} Try / catch
api, err := cliclient.NewClient(cmd)
if err != nil && strings.Contains(err.Error(), "ClientContextKey was expected") {
log.Error("wrong client type stored in context; store *client.OryKratos", "err", err)
} Prevention
- Only put *client.OryKratos under client.ClientContextKey.
- Use distinct context keys when embedding multiple Ory CLI libraries.
- In tests, stub the context value with the exact concrete type, not an interface/mock of another shape.
When it happens
Trigger: Embedding the Ory CLI commands in your own cobra command tree where code earlier in the chain stored a different type under the ClientContextKey (e.g. another API client or a raw configuration struct), then invoking a Kratos command.
Common situations: Composing Ory Keto/Hydra/Kratos CLI libraries together with a shared context key; tests stubbing the context value with a mock of the wrong type; refactors replacing the stored client type without updating the key.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- expected to get the DSN as an argument, or the…
- expected zero or two args, got
- api_key auth strategy requires a string name
- api_key auth strategy requires a string value
- basic_auth auth strategy requires a string user
AI-assisted analysis of ory/kratos@b86338da04 (2026-09-07).
Data as JSON: /api/errors/dbfafa2500a7c720.
Report an issue: GitHub.
Appendix: source
Thrown at cmd/cliclient/client.go:51
type ClientContext struct {
Endpoint string
HTTPClient *http.Client
}
func NewClient(cmd *cobra.Command) (*kratos.APIClient, error) {
if f, ok := cmd.Context().Value(ClientContextKey).(func(cmd *cobra.Command) (*ClientContext, error)); ok {
cc, err := f(cmd)
if err != nil {
return nil, err
}
conf := kratos.NewConfiguration()
conf.HTTPClient = cc.HTTPClient
conf.Servers = kratos.ServerConfigurations{{URL: cc.Endpoint}}
return kratos.NewAPIClient(conf), nil
} else if f != nil {
return nil, errors.Errorf("ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T ", f)
}
endpoint, err := cmd.Flags().GetString(FlagEndpoint)
if err != nil {
return nil, errors.WithStack(err)
}
if endpoint == "" {
endpoint = os.Getenv(envKeyEndpoint)
}
if endpoint == "" {
return nil, errors.Errorf("you have to set the remote endpoint, try --help for details")
}
u, err := url.Parse(endpoint)
if err != nil {
return nil, errors.Wrapf(err, `could not parse the endpoint URL "%s"`, endpoint)View on GitHub (pinned to b86338da04)