Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(csv-upload): allow mapping entire columns in case of dataset schema (#12141)

marliessophie2ddaf169e635
diff --git a/web/src/features/datasets/components/MappingCard.tsx b/web/src/features/datasets/components/MappingCard.tsx
index efe4cefb0f..176a231160 100644
--- a/web/src/features/datasets/components/MappingCard.tsx
+++ b/web/src/features/datasets/components/MappingCard.tsx
@@ -6,12 +6,19 @@ import {
} from "@/src/components/ui/card";
import { cn } from "@/src/utils/tailwind";
import { useDraggable, useDroppable } from "@dnd-kit/core";
-import { X } from "lucide-react";
+import { InfoIcon, X } from "lucide-react";
import {
type CsvColumnPreview,
type FieldMapping,
} from "@/src/features/datasets/lib/csv/types";
import { isSchemaField } from "@/src/features/datasets/lib/csv/helpers";
+import { Switch } from "@/src/components/ui/switch";
+import { Label } from "@/src/components/ui/label";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/src/components/ui/tooltip";
function SchemaKeyDropZone({
schemaKey,
@@ -146,6 +153,12 @@ type MappingCardProps = {
onRemoveInputColumn: (columnName: string, key?: string) => void;
onRemoveExpectedColumn: (columnName: string, key?: string) => void;
onRemoveMetadataColumn: (columnName: string) => void;
+ inputSchemaKeys: string[] | null;
+ expectedOutputSchemaKeys: string[] | null;
+ useDirectMappingForInput: boolean;
+ useDirectMappingForExpectedOutput: boolean;
+ onToggleDirectMappingForInput: (value: boolean) => void;
+ onToggleDirectMappingForExpectedOutput: (value: boolean) => void;
};
export function MappingCard({
@@ -155,6 +168,12 @@ export function MappingCard({
onRemoveInputColumn,
onRemoveExpectedColumn,
onRemoveMetadataColumn,
+ inputSchemaKeys,
+ expectedOutputSchemaKeys,
+ useDirectMappingForInput,
+ useDirectMappingForExpectedOutput,
+ onToggleDirectMappingForInput,
+ onToggleDirectMappingForExpectedOutput,
}: MappingCardProps) {
return (
<Card className="flex h-full flex-col overflow-hidden">
@@ -166,9 +185,37 @@ export function MappingCard({
<CardContent className="min-h-0 flex-1 space-y-4 overflow-y-auto p-3">
{/* INPUT SECTION */}
<div className="space-y-2">
- <h3 className="text-sm font-semibold tracking-wide text-muted-foreground">
- Input
- </h3>
+ <div className="flex items-center justify-between">
+ <h3 className="text-sm font-semibold tracking-wide text-muted-foreground">
+ Input
+ </h3>
+ {inputSchemaKeys && inputSchemaKeys.length > 0 && (
+ <div className="flex items-center gap-1.5">
+ <Switch
+ id="direct-mapping-input"
+ checked={useDirectMappingForInput}
+ onCheckedChange={onToggleDirectMappingForInput}
+ className="scale-75"
+ />
+ <Label
+ htmlFor="direct-mapping-input"
+ className="cursor-pointer text-xs font-normal text-muted-foreground"
+ >
+ Direct Mapping
+ </Label>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <InfoIcon className="h-3 w-3 text-muted-foreground" />
+ </TooltipTrigger>
+ <TooltipContent className="max-w-[250px]" side="left">
+ {useDirectMappingForInput
+ ? "Map entire CSV columns directly to the input field."
+ : "Map CSV columns to individual schema fields."}
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ )}
+ </div>
{isSchemaField(input) ? (
<div className="space-y-2">
{input.entries.map((entry) => (
@@ -194,9 +241,38 @@ export function MappingCard({
{/* OUTPUT SECTION */}
<div className="space-y-2">
- <h3 className="text-sm font-semibold tracking-wide text-muted-foreground">
- Expected Output
- </h3>
+ <div className="flex items-center justify-between">
+ <h3 className="text-sm font-semibold tracking-wide text-muted-foreground">
+ Expected Output
+ </h3>
+ {expectedOutputSchemaKeys &&
+ expectedOutputSchemaKeys.length > 0 && (
+ <div className="flex items-center gap-1.5">
+ <Switch
+ id="direct-mapping-expected"
+ checked={useDirectMappingForExpectedOutput}
+ onCheckedChange={onToggleDirectMappingForExpectedOutput}
+ className="scale-75"
+ />
+ <Label
+ htmlFor="direct-mapping-expected"
+ className="cursor-pointer text-xs font-normal text-muted-foreground"
+ >
+ Direct mapping
+ </Label>
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <InfoIcon className="h-3 w-3 text-muted-foreground" />
+ </TooltipTrigger>
+ <TooltipContent className="max-w-[250px]" side="left">
+ {useDirectMappingForExpectedOutput
+ ? "Map entire CSV columns directly to the expected output field."
+ : "Map CSV columns to individual schema fields."}
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ )}
+ </div>
{expectedOutput.type === "schema" ? (
<div className="space-y-2">
{expectedOutput.entries.map((entry) => (
diff --git a/web/src/features/datasets/components/PreviewCsvImport.tsx b/web/src/features/datasets/components/PreviewCsvImport.tsx
index 886d52e372..575ea5f1e0 100644
--- a/web/src/features/datasets/components/PreviewCsvImport.tsx
+++ b/web/src/features/datasets/components/PreviewCsvImport.tsx
@@ -6,7 +6,7 @@ import {
MeasuringStrategy,
DragOverlay,
} from "@dnd-kit/core";
-import { useState } from "react";
+import { useState, useEffect } from "react";
import { Button } from "@/src/components/ui/button";
import { api } from "@/src/utils/api";
import { Progress } from "@/src/components/ui/progress";
@@ -66,13 +66,35 @@ export function PreviewCsvImport({
dataset?.expectedOutputSchema,
);
+ // Toggle states for direct mapping mode (per field)
+ const [useDirectMappingForInput, setUseDirectMappingForInput] =
+ useState(false);
+ const [
+ useDirectMappingForExpectedOutput,
+ setUseDirectMappingForExpectedOutput,
+ ] = useState(false);
+
+ // Compute effective schema keys - pass undefined when in direct mapping mode
+ const effectiveInputSchemaKeys = useDirectMappingForInput
+ ? undefined
+ : (inputSchemaKeys ?? undefined);
+ const effectiveExpectedOutputSchemaKeys = useDirectMappingForExpectedOutput
+ ? undefined
+ : (expectedOutputSchemaKeys ?? undefined);
+
// Mapping state
const mapping = useCsvMapping({
preview,
- inputSchemaKeys: inputSchemaKeys ?? undefined,
- expectedOutputSchemaKeys: expectedOutputSchemaKeys ?? undefined,
+ inputSchemaKeys: effectiveInputSchemaKeys,
+ expectedOutputSchemaKeys: effectiveExpectedOutputSchemaKeys,
});
+ // Reset mappings when switching modes (avoids stale closure in callback)
+ useEffect(() => {
+ mapping.reset();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [useDirectMappingForInput, useDirectMappingForExpectedOutput]);
+
// Drag and drop
const dragAndDrop = useCsvDragAndDrop({
handlers: {
@@ -113,10 +135,9 @@ export function PreviewCsvImport({
const handleImport = async () => {
capture("dataset_item:upload_csv_form_submit");
- await csvImport.execute(wrapSingleColumn);
+ const success = await csvImport.execute(wrapSingleColumn);
- // Close dialog on success
- if (csvImport.progress.status === "complete") {
+ if (success) {
setOpen?.(false);
setPreview(null);
}
@@ -158,6 +179,16 @@ export function PreviewCsvImport({
onRemoveInputColumn={mapping.removeColumnFromInput}
onRemoveExpectedColumn={mapping.removeColumnFromExpectedOutput}
onRemoveMetadataColumn={mapping.removeColumnFromMetadata}
+ inputSchemaKeys={inputSchemaKeys}
+ expectedOutputSchemaKeys={expectedOutputSchemaKeys}
+ useDirectMappingForInput={useDirectMappingForInput}
+ useDirectMappingForExpectedOutput={
+ useDirectMappingForExpectedOutput
+ }
+ onToggleDirectMappingForInput={setUseDirectMappingForInput}
+ onToggleDirectMappingForExpectedOutput={
+ setUseDirectMappingForExpectedOutput
+ }
/>
</div>
{createPortal(
@@ -195,7 +226,10 @@ export function PreviewCsvImport({
)}
</DialogBody>
<DialogFooter>
- {!isSchemaMode && (
+ {/* Show checkbox in freeform mode OR when using direct mapping for any field */}
+ {(useDirectMappingForInput ||
+ useDirectMappingForExpectedOutput ||
+ !isSchemaMode) && (
<div className="flex items-center gap-2">
<Checkbox
id="wrapSingleColumn"
diff --git a/web/src/features/datasets/hooks/useCsvImport.ts b/web/src/features/datasets/hooks/useCsvImport.ts
index e9b363c3c3..4ba0fee500 100644
--- a/web/src/features/datasets/hooks/useCsvImport.ts
+++ b/web/src/features/datasets/hooks/useCsvImport.ts
@@ -79,14 +79,14 @@ export function useCsvImport(options: UseCsvImportOptions) {
const mutCreateManyDatasetItems =
api.datasets.createManyDatasetItems.useMutation({});
- const execute = async (wrapSingleColumn: boolean) => {
+ const execute = async (wrapSingleColumn: boolean): Promise<boolean> => {
const { csvFile, projectId, datasetId, input, expectedOutput, metadata } =
options;
- if (!csvFile) return;
+ if (!csvFile) return false;
if (csvFile.size > MAX_FILE_SIZE_BYTES) {
showErrorToast("File too large", "Maximum file size is 10MB");
- return;
+ return false;
}
setValidationErrors([]);
@@ -219,7 +219,7 @@ export function useCsvImport(options: UseCsvImportOptions) {
processedItems: 0,
status: "not-started",
});
- return;
+ return false;
}
processedCount += chunkItems.length;
@@ -248,7 +248,7 @@ export function useCsvImport(options: UseCsvImportOptions) {
`Please try again starting from row ${processedCount + 1}.`,
);
}
- return;
+ return false;
}
utils.datasets.invalidate();
@@ -258,6 +258,8 @@ export function useCsvImport(options: UseCsvImportOptions) {
processedItems: items.length,
status: "complete",
});
+
+ return true;
};
const reset = () => {