MiniMax-AI/skills · error · InvalidOperationException
Relationship {oldRelId} does not point to an ImagePart.
Error message
Relationship {oldRelId} does not point to an ImagePart. What it means
`throw new InvalidOperationException($"Relationship {oldRelId} does not point to an ImagePart.")` in `ImageSamples.ReplaceExistingImage`. The method looks up a part by relationship id (`mainPart.GetPartById(oldRelId)`) and checks whether it is an `ImagePart`. If the rId points to a different part type (hyperlink, stylesheet, header, embedded object) the replace cannot proceed because `FeedData` would corrupt unrelated content.
Source
Thrown at skills/minimax-docx/scripts/dotnet/MiniMaxAIDocx.Core/Samples/ImageSamples.cs:500
// ── 8. Replace Existing Image ──────────────────────────────────────
/// <summary>
/// Replaces an existing image by updating the ImagePart data behind a
/// known relationship ID. The Blip.Embed attribute (rId) stays the same;
/// only the binary content changes. This avoids needing to rebuild the
/// entire Drawing XML tree.
/// </summary>
/// <param name="mainPart">The MainDocumentPart containing the image relationship.</param>
/// <param name="oldRelId">The existing relationship ID (e.g., "rId5") of the image to replace.</param>
/// <param name="newImagePath">Path to the replacement image file.</param>
public static void ReplaceExistingImage(
MainDocumentPart mainPart, string oldRelId, string newImagePath)
{
// Look up the existing ImagePart by its relationship ID
OpenXmlPart part = mainPart.GetPartById(oldRelId);
if (part is not ImagePart imagePart)
{
throw new InvalidOperationException(
$"Relationship {oldRelId} does not point to an ImagePart.");
}
// Feed new image data into the existing part.
// This replaces the binary content while keeping the same rId.
using (FileStream stream = new FileStream(newImagePath, FileMode.Open))
{
imagePart.FeedData(stream);
}
// NOTE: If the new image has different dimensions, you should also
// update the Extent.Cx/Cy and A.Extents.Cx/Cy in the Drawing element.
// Find all Blip elements referencing this relId:
//
// var blips = mainPart.Document.Descendants<A.Blip>()
// .Where(b => b.Embed == oldRelId);
// foreach (var blip in blips)
// {View on GitHub (pinned to 60aaae52bb)
Solutions
- Extract the rId from the image's `a:blip r:embed` (Drawing) element, not from hyperlinks or other relationships.
- Before calling, verify with `mainPart.GetPartById(oldRelId) is ImagePart`.
- Enumerate `mainPart.ImageParts` to find the correct rId for the image you intend to replace.
Example fix
// before — rId came from a hyperlink, not the image blip ImageSamples.ReplaceExistingImage(mainPart, "rId3", newPath); // rId3 is a hyperlink // after — use the blip's r:embed value // <a:blip r:embed="rId5"/> ImageSamples.ReplaceExistingImage(mainPart, "rId5", newPath);
Defensive patterns
Strategy: type-guard
Validate before calling
void ReplaceImage(MainDocumentPart mainPart, string oldRelId, string newPath)
{
if (mainPart.GetPartById(oldRelId) is not ImagePart)
throw new ArgumentException($"{oldRelId} is not an image relationship.", nameof(oldRelId));
ImageSamples.ReplaceExistingImage(mainPart, oldRelId, newPath);
} Type guard
bool IsImageRelationship(MainDocumentPart mainPart, string relId) =>
mainPart.GetPartById(relId) is ImagePart; Try / catch
try
{
ImageSamples.ReplaceExistingImage(mainPart, oldRelId, newImagePath);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("ImagePart"))
{
// find the real image rId from the Drawing's a:blip r:embed
var realRid = FindBlipEmbedId(mainPart);
ImageSamples.ReplaceExistingImage(mainPart, realRid, newImagePath);
} Prevention
- Always read the rId from the image's `<a:blip r:embed="...">`, never from hyperlinks or other relationships.
- Pre-check `mainPart.GetPartById(relId) is ImagePart` before calling.
- Enumerate `mainPart.ImageParts` to discover valid image rIds.
- Log the part type when the check fails so the wrong source element is obvious.
When it happens
Trigger: Passing an `oldRelId` that exists but references a non-image relationship — e.g. a hyperlink rId, a styles/header/footer relationship, or a media relationship that is not an `ImagePart`. Also passing a stale rId copied from the wrong element.
Common situations: Reading an rId off the wrong XML element (e.g. a `w:hyperlink r:id` instead of an `a:blip r:embed`), an rId from an older version of the document, or assuming every rId in `document.xml` is an image.
Related errors
- Image format '{ext}' is not supported by OpenXML.
- Document has no comments part.
- Missing word/document.xml
- DOCX does not contain word/document.xml
- level
AI-assisted analysis of MiniMax-AI/skills@60aaae52bb (2026-08-13).
Data as JSON: /api/errors/360361f329802d8d.
Report an issue: GitHub.