{"record":{"id":"8e60a704ed3fa3fc","repo":"dotnet/machinelearning","slug":"either-input-ids-or-inputs-embeds-must-be-set","errorCode":null,"errorMessage":"Either input_ids or inputs_embeds must be set","messagePattern":"Either input_ids or inputs_embeds must be set","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.GenAI.LLaMA/Module/LlamaModel.cs","lineNumber":93,"sourceCode":"        {\n            throw new ArgumentException(\"Only one of input_ids or inputs_embeds may be set\");\n        }\n        else if (inputIds is not null)\n        {\n            batchSize = inputIds.IntShape()[0];\n            seqLength = inputIds.IntShape()[1];\n            inputsEmbeds = this.embed_tokens.forward(inputIds);\n            device = inputIds.device;\n        }\n        else if (inputsEmbeds is not null)\n        {\n            batchSize = inputsEmbeds.IntShape()[0];\n            seqLength = inputsEmbeds.IntShape()[1];\n            device = inputsEmbeds.device;\n        }\n        else\n        {\n            throw new ArgumentException(\"Either input_ids or inputs_embeds must be set\");\n        }\n\n        var pastKeyValuesLength = input.PastKeyValuesLength;\n\n        if (positionIds is null)\n        {\n            positionIds = torch.arange(pastKeyValuesLength, seqLength + pastKeyValuesLength, device: device);\n            positionIds = positionIds.unsqueeze(0).view(-1, seqLength);\n        }\n        else\n        {\n            positionIds = ((long)positionIds.view(-1, seqLength));\n        }\n\n        if (this._config.AttnImplementation == \"flash_attention_2\")\n        {\n            throw new NotImplementedException();\n        }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.GenAI.LLaMA/Module/LlamaModel.cs#L75-L111","documentation":"Validation in LlamaModel.forward: exactly one of input_ids or inputs_embeds must be supplied. If both are given, a separate ArgumentException fires (\"Only one of ...\"); if neither is given, execution reaches the final else and throws this error stating that at least one is required, since the forward pass has no input to embed or attend over.","triggerScenarios":"Calling LlamaModel.forward with a LlamaModelInput whose InputIds and InputEmbeddings are both null (e.g. only PositionIds/attention mask set).","commonSituations":"Building custom inference loops where the input struct is created empty and fields are filled conditionally, with a branch that never executes; or deserializing a request that dropped the token field.","solutions":["Set InputIds (token ids from the tokenizer) on the input object.","Alternatively set InputEmbeddings to precomputed embeddings.","Add a null check on the input before calling forward to fail fast with a clearer message."],"exampleFix":"// before\nvar input = new LlamaModelInput { AttentionMask = mask };\nvar output = model.forward(input); // throws\n// after\nvar input = new LlamaModelInput { InputIds = tokenizer.Encode(prompt), AttentionMask = mask };\nvar output = model.forward(input);","handlingStrategy":"validation","validationCode":"if (input.InputIds is null && input.InputEmbeddings is null) throw new ArgumentException(\"Input requires InputIds or InputEmbeddings.\");","typeGuard":"bool HasNoInput(LlamaModelInput i) => i.InputIds is null && i.InputEmbeddings is null;","tryCatchPattern":"try { output = model.forward(input); } catch (ArgumentException) { /* tokenize the prompt and set InputIds, then retry */ }","preventionTips":["Always tokenize the prompt into InputIds before building the input object","Validate the input object in a helper factory instead of constructing inline","Log the input fields when building generation loops"],"tags":["argument-validation","llama","missing-input"],"backgroundTag":"missing-required-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}