{"record":{"id":"dbfafa2500a7c720","repo":"ory/kratos","slug":"clientcontextkey-was-expected-to-be-client-orykra","errorCode":null,"errorMessage":"ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T ","messagePattern":"ClientContextKey was expected to be \\*client\\.OryKratos but it contained an invalid type %T ","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cliclient/client.go","lineNumber":51,"sourceCode":"\ntype ClientContext struct {\n\tEndpoint   string\n\tHTTPClient *http.Client\n}\n\nfunc NewClient(cmd *cobra.Command) (*kratos.APIClient, error) {\n\tif f, ok := cmd.Context().Value(ClientContextKey).(func(cmd *cobra.Command) (*ClientContext, error)); ok {\n\t\tcc, err := f(cmd)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tconf := kratos.NewConfiguration()\n\t\tconf.HTTPClient = cc.HTTPClient\n\t\tconf.Servers = kratos.ServerConfigurations{{URL: cc.Endpoint}}\n\t\treturn kratos.NewAPIClient(conf), nil\n\t} else if f != nil {\n\t\treturn nil, errors.Errorf(\"ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T \", f)\n\t}\n\n\tendpoint, err := cmd.Flags().GetString(FlagEndpoint)\n\tif err != nil {\n\t\treturn nil, errors.WithStack(err)\n\t}\n\n\tif endpoint == \"\" {\n\t\tendpoint = os.Getenv(envKeyEndpoint)\n\t}\n\n\tif endpoint == \"\" {\n\t\treturn nil, errors.Errorf(\"you have to set the remote endpoint, try --help for details\")\n\t}\n\n\tu, err := url.Parse(endpoint)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, `could not parse the endpoint URL \"%s\"`, endpoint)","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/ory/kratos/blob/b86338da04a040247a07f46100a86dcfb3875909/cmd/cliclient/client.go#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nctx = context.WithValue(ctx, client.ClientContextKey, myKetoClient)\n// after\nctx = context.WithValue(ctx, client.ClientContextKey, kratos.NewAPIClient(kratos.NewConfiguration()))","handlingStrategy":"type-guard","validationCode":"v := cmd.Context().Value(client.ClientContextKey)\nif v != nil {\n    if _, ok := v.(*client.OryKratos); !ok {\n        return fmt.Errorf(\"context client has wrong type %T\", v)\n    }\n}","typeGuard":"func kratosFromContext(ctx context.Context) (*client.OryKratos, bool) {\n    c, ok := ctx.Value(client.ClientContextKey).(*client.OryKratos)\n    return c, ok\n}","tryCatchPattern":"api, err := cliclient.NewClient(cmd)\nif err != nil && strings.Contains(err.Error(), \"ClientContextKey was expected\") {\n    log.Error(\"wrong client type stored in context; store *client.OryKratos\", \"err\", err)\n}","preventionTips":["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."],"tags":["context","type-mismatch","cli","client"],"backgroundTag":"type-mismatch","analyzedSha":"b86338da04a040247a07f46100a86dcfb3875909","analyzedAt":"2026-09-07T15:58:15.934Z","contentChangedAt":"2026-09-07T15:58:15.934Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}