digitalocean / digitalocean/langchain-gradient

Implement Structured Output with Pydantic Models

Open
#8 14 comments 0 reactions 0 assignees View on GitHub
hacktoberfest
Dominant language
Python
Stars
15
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Add support for structured outputs using Pydantic models, similar to OpenAI's with_structured_output() method.

**Acceptance Criteria:**

- Add with_structured_output() method to ChatGradient
- Support Pydantic model validation
- Handle JSON parsing and validation errors gracefully
- Support both single and multiple structured outputs
- Add comprehensive tests for structured output functionality
- Update documentation with structured output examples

**Technical Requirements:**

- Use Pydantic for model validation
- Support both response_format parameter and with_structured_output() method
- Handle validation errors with clear error messages
- Maintain type safety

**Example Usage:**

from pydantic import BaseModel

class Person(BaseModel):
name: str
age: int
email: str

llm = ChatGradient(model="llama3.3-70b-instruct")
structured_llm = llm.with_structured_output(Person)
response = structured_llm.invoke("Create a person named John, age 30, email john@example.com")
# Returns: Person(name="John", age=30, email="john@example.com")

**References:**

[OpenAI Structured Output Documentation](https://python.langchain.com/docs/integrations/chat/openai/#structured-output)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.