{"record":{"id":"4537b16cf27fa289","repo":"dotnet/machinelearning","slug":"only-one-of-input-ids-or-inputs-embeds-may-be-set-4537b1","errorCode":null,"errorMessage":"Only one of input_ids or inputs_embeds may be set","messagePattern":"Only one of input_ids or inputs_embeds may be set","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.GenAI.Phi/Module/Phi3Model.cs","lineNumber":73,"sourceCode":"#pragma warning restore MSML_GeneralName // This name should be PascalCased\n    {\n        if (input.OverrideCache is not null)\n        {\n            this._cache = input.OverrideCache;\n        }\n\n        var outputAttentions = input.OutputAttentions;\n        var outputHiddenStates = input.OutputHiddenStates;\n        var attentionMask = input.AttentionMask;\n        Device device;\n        var inputIds = input.InputIds;\n        var positionIds = input.PositionIds;\n        var inputsEmbeds = input.InputEmbeddings;\n        int batchSize;\n        int seqLength;\n        if (inputIds is not null && inputsEmbeds is not null)\n        {\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        }","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.GenAI.Phi/Module/Phi3Model.cs#L55-L91","documentation":"Phi3Model.forward accepts either input token ids or precomputed input embeddings, but not both. Supplying both triggers ArgumentException('Only one of input_ids or inputs_embeds may be set'), mirroring the transformers mutual-exclusion check.","triggerScenarios":"Calling Phi3Model.forward with input.InputEmbeddings set and inputIds also set (non-null) — e.g. a pipeline that precomputes embeddings while still passing token ids.","commonSituations":"Caching embeddings for speed while also forwarding ids; generic inference harnesses that populate both fields by default; migration from Phi2Model (which rejected embeddings entirely) while leaving ids in place.","solutions":["Pass only one: null out inputIds when supplying InputEmbeddings, or vice versa","If you want embedding-based input, clear InputIds on the input struct/record","Add a pre-call check mirroring the mutual exclusion and choose ids as the canonical input"],"exampleFix":"// before\nmodel.forward(inputIds: ids, input: new Phi3Input { InputEmbeddings = embeds, ... });\n// after\nmodel.forward(inputIds: null, input: new Phi3Input { InputEmbeddings = embeds, ... });","handlingStrategy":"validation","validationCode":"if (inputIds is not null && input.InputEmbeddings is not null)\n    inputIds = null; // or throw, depending on desired strictness","typeGuard":"static bool HasExactlyOneInput(TorchTensor? ids, TorchTensor? embeds) => (ids is null) ^ (embeds is null);","tryCatchPattern":"try { output = model.forward(inputIds, input, options); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Only one of input_ids or inputs_embeds\")) { throw new InvalidOperationException(\"Provide either input_ids or inputs_embeds, not both\", ex); }","preventionTips":["Choose one canonical input path (ids) unless you explicitly need embeddings","Clear InputIds when supplying InputEmbeddings in pipeline code","Assert mutual exclusion at the boundary before calling the model"],"tags":["csharp","phi-3","mutually-exclusive"],"backgroundTag":"mutually-exclusive-options","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"}