Tencent/WeKnora · error

write file content: %w

Error message

write file content: %w

What it means

Writing the uploaded document's bytes into the multipart field failed (part.Write(content)). This surfaces when the in-memory pipe/buffer backing the multipart writer fails, e.g. because the request body writer errored or memory allocation failed for very large files.

Source

Thrown at internal/infrastructure/docparser/mineru_converter.go:232

	if c.language != "" {
		fields["lang_list"] = c.language
	}
	if c.vlmServerURL != "" && (strings.HasPrefix(c.backend, "vlm-http-client") || strings.HasPrefix(c.backend, "hybrid-http-client")) {
		fields["server_url"] = c.vlmServerURL
	}
	for k, v := range fields {
		_ = writer.WriteField(k, v)
	}

	uploadFileName := minerUUploadFileName(fileName, fileType)

	// File part
	part, err := writer.CreateFormFile("files", uploadFileName)
	if err != nil {
		return "", nil, fmt.Errorf("create form file: %w", err)
	}
	if _, err := part.Write(content); err != nil {
		return "", nil, fmt.Errorf("write file content: %w", err)
	}
	writer.Close()

	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint+"/file_parse", &body)
	if err != nil {
		return "", nil, fmt.Errorf("create request: %w", err)
	}
	httpReq.Header.Set("Content-Type", writer.FormDataContentType())

	client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{
		Timeout:      mineruTimeout,
		MaxRedirects: 5,
	})
	resp, err := client.Do(httpReq)
	if err != nil {
		return "", nil, fmt.Errorf("HTTP request: %w", err)
	}
	defer resp.Body.Close()

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Check the document size against reasonable limits before calling Read (reject/limit huge files earlier in the pipeline).
  2. Confirm the multipart writer and its backing buffer are used correctly (no early Close, single goroutine).
  3. Inspect host memory (OOM logs, dmesg) if this occurs with large PDFs.
  4. Check that content passed into Read is fully valid (not a zero-length/corrupt slice from an earlier failed read).
Defensive patterns

Strategy: validation

Validate before calling

if len(content) == 0 {
    return fmt.Errorf("refusing to send empty document to MinerU")
}
if maxDocSize > 0 && len(content) > maxDocSize {
    return fmt.Errorf("document too large for MinerU: %d bytes", len(content))
}

Try / catch

if err != nil && strings.Contains(err.Error(), "write file content") {
    // in-memory failure: reject the document rather than retry
    return fmt.Errorf("document rejected (write failed): %w", err)
}

Prevention

When it happens

Trigger: part.Write(content) returns n < len(content) with a non-nil error while callFileParse assembles the form for the /file_parse request.

Common situations: Extremely large documents exhausting memory (body is an in-memory buffer); a custom body writer (bytes.Buffer/pipe) closed prematurely; OOM pressure on the host causing allocation failures.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/75fa2459cc7f23b9. Report an issue: GitHub.