Document Moderation
The SDK provides comprehensive functionality for moderating document processing results. This guide covers all available methods for document moderation, fully aligned with the Nanonets API.
Setup
Minimum Go version required: 1.18
Install the Nanonets Go SDK:
go get github.com/nanonets/nanonets-go-sdk
Field Management
Update Field Value
Updates a specific field value in a document page.
import "github.com/NanoNets/nanonets-go/nanonets"
client := nanonets.NewClient("your_api_key")
err := client.Moderation.UpdateField(
"workflow_123",
"document_123",
"page_123",
"field_data_123",
nanonets.UpdateFieldRequest{
Value: "INV-2024-003",
},
)
if err != nil {
// Handle error
}
Add Field Value
Adds a new field value to a document page.
err := client.Moderation.AddField(
"workflow_123",
"document_123",
"page_123",
nanonets.AddFieldRequest{
FieldName: "invoice_number",
Value: "INV-2024-004",
Bbox: []float64{100, 200, 300, 250},
Confidence: 1.0,
VerificationStatus: "unverified",
VerificationMessage: "",
},
)
if err != nil {
// Handle error
}
Delete Field Value
Removes a field value from a document page.
err := client.Moderation.DeleteField(
"workflow_123",
"document_123",
"page_123",
"field_data_123",
)
if err != nil {
// Handle error
}
Table Management
Add Table
Adds a new table to a document page.
err := client.Moderation.AddTable(
"workflow_123",
"document_123",
"page_123",
nanonets.AddTableRequest{
Bbox: []float64{100, 300, 800, 600},
Headers: []string{"item_description", "quantity", "price"},
VerificationStatus: "unverified",
VerificationMessage: "",
Cells: []nanonets.Cell{
{
Row: 0,
Col: 0,
Header: "item_description",
Text: "Product A",
Bbox: []float64{100, 330, 300, 360},
VerificationStatus: "unverified",
VerificationMessage: "",
},
// ... more cells ...
},
},
)
if err != nil {
// Handle error
}
Update Table Cell
Updates a specific cell in a table.
err := client.Moderation.UpdateTableCell(
"workflow_123",
"document_123",
"page_123",
"table_123",
"cell_123",
nanonets.UpdateTableCellRequest{
Value: "Product B",
},
)
if err != nil {
// Handle error
}
Add Table Cell
Adds a new cell to a table.
err := client.Moderation.AddTableCell(
"workflow_123",
"document_123",
"page_123",
"table_123",
nanonets.AddTableCellRequest{
Row: 0,
Col: 0,
Header: "item_description",
Text: "Product A",
Bbox: []float64{100, 330, 300, 360},
VerificationStatus: "unverified",
VerificationMessage: "",
},
)
if err != nil {
// Handle error
}
Delete Table Cell
Removes a cell from a table.
err := client.Moderation.DeleteTableCell(
"workflow_123",
"document_123",
"page_123",
"table_123",
"cell_123",
)
if err != nil {
// Handle error
}
Verification
Verify Field
Marks a field as verified.
err := client.Moderation.VerifyField(
"workflow_123",
"document_123",
"page_123",
"field_data_123",
nanonets.VerifyFieldRequest{
VerificationStatus: "verified",
VerificationMessage: "Field value is correct",
},
)
if err != nil {
// Handle error
}
Verify Table Cell
Marks a table cell as verified.
err := client.Moderation.VerifyTableCell(
"workflow_123",
"document_123",
"page_123",
"table_123",
"cell_123",
nanonets.VerifyTableCellRequest{
VerificationStatus: "verified",
VerificationMessage: "Cell value is correct",
},
)
if err != nil {
// Handle error
}
Verify Table
Marks an entire table as verified.
err := client.Moderation.VerifyTable(
"workflow_123",
"document_123",
"page_123",
"table_123",
nanonets.VerifyTableRequest{
VerificationStatus: "verified",
VerificationMessage: "Table data is correct",
},
)
if err != nil {
// Handle error
}
Verify Document
Marks an entire document as verified.
err := client.Moderation.VerifyDocument(
"workflow_123",
"document_123",
nanonets.VerifyDocumentRequest{
VerificationStatus: "verified",
VerificationMessage: "Document data is correct",
},
)
if err != nil {
// Handle error
}
Error Handling & Common Scenarios
API error codes:
- 200 OK: Request successful
- 400 Bad Request: Invalid request parameters
- 401 Unauthorized: Invalid/missing API key
- 404 Not Found: Workflow, document, page, field, or table not found
- 500 Internal Server Error: Server-side error
Common error scenarios:
- Invalid field/table IDs
- Invalid verification status values
- Missing required parameters
- Permission issues
err := client.Moderation.UpdateField(...)
if err != nil {
switch {
case strings.Contains(err.Error(), "401"):
fmt.Println("Authentication failed:", err)
case strings.Contains(err.Error(), "404"):
fmt.Println("Resource not found:", err)
case strings.Contains(err.Error(), "400"):
fmt.Println("Invalid input:", err)
default:
fmt.Println("An error occurred:", err)
}
}
Best Practices
Batch Verification
func verifyDocumentFields(client *nanonets.Client, workflowID, documentID string, fields []string) error {
for _, fieldID := range fields {
err := client.Moderation.VerifyField(
workflowID,
documentID,
"page_123",
fieldID,
nanonets.VerifyFieldRequest{
VerificationStatus: "verified",
VerificationMessage: "Verified in batch",
},
)
if err != nil {
return fmt.Errorf("failed to verify field %s: %v", fieldID, err)
}
}
return nil
}Table Validation
func validateTable(client *nanonets.Client, workflowID, documentID, pageID, tableID string) error {
// First verify all cells
cells, err := client.Documents.GetTables(workflowID, documentID)
if err != nil {
return err
}
for _, table := range cells {
if table.TableID == tableID {
for _, cell := range table.Cells {
err := client.Moderation.VerifyTableCell(
workflowID,
documentID,
pageID,
tableID,
cell.CellID,
nanonets.VerifyTableCellRequest{
VerificationStatus: "verified",
VerificationMessage: "Cell validated",
},
)
if err != nil {
return err
}
}
// Then verify the entire table
return client.Moderation.VerifyTable(
workflowID,
documentID,
pageID,
tableID,
nanonets.VerifyTableRequest{
VerificationStatus: "verified",
VerificationMessage: "Table validated",
},
)
}
}
return fmt.Errorf("table not found")
}Error Recovery
func verifyWithRetry(client *nanonets.Client, workflowID, documentID, pageID, fieldID string) error {
var err error
for i := 0; i < 3; i++ { // 3 retries
err = client.Moderation.VerifyField(
workflowID,
documentID,
pageID,
fieldID,
nanonets.VerifyFieldRequest{
VerificationStatus: "verified",
VerificationMessage: "Verified with retry",
},
)
if err == nil {
return nil
}
time.Sleep(time.Second * time.Duration(i+1))
}
return err
}