feat: add DataFrame output to Structured Output component (#8842)

* feat: add DataFrame output to Structured Output component

- Add new DataFrame output alongside existing Data output for structured data
- Single output: creates DataFrame with one row
- Multiple outputs: creates DataFrame with multiple rows, each containing a Data object
- Maintains backward compatibility with existing Data output
- Includes comprehensive test coverage for both single and multiple output scenarios

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes

* Update structured_output.py

* [autofix.ci] apply automated fixes

* udpate to tests

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* update to format instructions

* Update system prompt in structured output component test

Expanded the system prompt in the test for StructuredOutputComponent to provide more detailed extraction instructions, including handling of missing values, duplicates, and output format requirements. This improves test clarity and better simulates real-world extraction scenarios.

* [autofix.ci] apply automated fixes

* Update structured_output.py

* fix: return empty Data object instead of None in StructuredOutputComponent

Updated the return statement in the StructuredOutputComponent to return an empty Data object when there are no outputs, improving consistency in the output handling.

* [autofix.ci] apply automated fixes

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Edwin Jose <edwin.jose@datastax.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
Rodrigo Nader 2025-07-09 18:57:07 -03:00 • committed by GitHub
commit a0e484c0eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 424 additions and 18 deletions

View file

@ -12,6 +12,7 @@ from langflow.io import (
TableInput,
)
from langflow.schema.data import Data
from langflow.schema.dataframe import DataFrame
from langflow.schema.table import EditMode
@ -42,13 +43,13 @@ class StructuredOutputComponent(Component):
display_name="Format Instructions",
info="The instructions to the language model for formatting the output.",
value=(
"You are an AI that extracts one structured JSON object from unstructured text. "
"You are an AI that extracts structured JSON objects from unstructured text. "
"Use a predefined schema with expected types (str, int, float, bool, dict). "
"If multiple structures exist, extract only the first most complete one. "
"Extract ALL relevant instances that match the schema - if multiple patterns exist, capture them all. "
"Fill missing or ambiguous values with defaults: null for missing values. "
"Ignore duplicates and partial repeats. "
"Always return one valid JSON, never throw errors or return multiple objects."
"Output: A single well-formed JSON object, and nothing else."
"Remove exact duplicates but keep variations that have different field values. "
"Always return valid JSON in the expected format, never throw errors. "
"If multiple objects can be extracted, return them all in the structured format."
),
required=True,
advanced=True,
@ -117,6 +118,11 @@ class StructuredOutputComponent(Component):
display_name="Structured Output",
method="build_structured_output",
),
Output(
name="dataframe_output",
display_name="Structured Output",
method="build_structured_dataframe",
),
]
def build_structured_output_base(self):
@ -178,7 +184,19 @@ class StructuredOutputComponent(Component):
# handle empty or unexpected type case
msg = "No structured output returned"
raise ValueError(msg)
if len(output) != 1:
msg = "Multiple structured outputs returned"
if len(output) == 1:
return Data(data=output[0])
if len(output) > 1:
# Multiple outputs - wrap them in a results container
return Data(data={"results": output})
return Data()
def build_structured_dataframe(self) -> DataFrame:
output = self.build_structured_output_base()
if not isinstance(output, list) or not output:
# handle empty or unexpected type case
msg = "No structured output returned"
raise ValueError(msg)
return Data(data=output[0])
data_list = [Data(data=output[0])] if len(output) == 1 else [Data(data=item) for item in output]
return DataFrame(data_list)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long