or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

chat-interface.mdclient-management.mddocument-prompt-template.mdembeddings.mdevaluation.mdexplanations.mdindex.mdprompt-construction.mdsteering.mdstructured-output.mdtext-completion.mdtokenization.mdtranslation.mdutilities.md

structured-output.mddocs/

0

# Structured Output

1

2

Generate structured JSON output from language models that conforms to specified schemas. Provides type-safe response parsing and validation with support for both manual JSON schemas and automatic Pydantic model integration.

3

4

## Capabilities

5

6

### JSON Schema Definition

7

8

Define structured output schemas with validation and type enforcement.

9

10

```python { .api }

11

class JSONSchema:

12

schema: Mapping[str, Any]

13

name: str

14

description: Optional[str] = None

15

strict: Optional[bool] = False

16

17

def __init__(

18

self,

19

schema: Mapping[str, Any],

20

name: str,

21

description: Optional[str] = None,

22

strict: Optional[bool] = False

23

):

24

"""

25

JSON schema that structured output must adhere to.

26

27

Parameters:

28

- schema: JSON schema that structured output must adhere to

29

- name: Name of the schema

30

- description: Description of the schema

31

- strict: Whether the schema should be strictly enforced

32

"""

33

34

def to_json(self) -> Mapping[str, Any]:

35

"""Convert schema to JSON format for API requests."""

36

37

@classmethod

38

def from_pydantic(cls, model_class: Type[BaseModel]) -> JSONSchema:

39

"""

40

Create a JSONSchema from a Pydantic model class.

41

42

Parameters:

43

- model_class: A Pydantic BaseModel class

44

45

Returns:

46

JSONSchema instance with schema generated from the Pydantic model

47

48

Raises:

49

ValueError: If the provided class is not a Pydantic BaseModel

50

"""

51

```

52

53

### Response Format Types

54

55

Type alias for structured output response formats.

56

57

```python { .api }

58

ResponseFormat = Union[JSONSchema, Type[BaseModel]]

59

```

60

61

### Usage Examples

62

63

Creating and using structured output schemas:

64

65

```python

66

from aleph_alpha_client import JSONSchema, ChatRequest, Client

67

from pydantic import BaseModel

68

from typing import List

69

70

client = Client(token="your-api-token")

71

72

# Manual JSON schema

73

schema = JSONSchema(

74

schema={

75

'type': 'object',

76

'title': 'PersonInfo',

77

'properties': {

78

'name': {

79

'type': 'string',

80

'title': 'Name'

81

},

82

'age': {

83

'type': 'integer',

84

'title': 'Age'

85

},

86

'skills': {

87

'type': 'array',

88

'items': {'type': 'string'},

89

'title': 'Skills'

90

}

91

},

92

'required': ['name', 'age']

93

},

94

name="person_info",

95

description="Extract person information",

96

strict=True

97

)

98

99

# Use in chat request

100

request = ChatRequest(

101

model="pharia-1-llm-7b-control",

102

messages=[{

103

"role": "user",

104

"content": "Extract the person's information: John is 30 years old and knows Python and JavaScript"

105

}],

106

response_format=schema

107

)

108

109

response = client.chat(request)

110

print(response.message.content) # Structured JSON output

111

112

# Using Pydantic models (automatic schema generation)

113

class PersonInfo(BaseModel):

114

"""Person information extraction model."""

115

name: str

116

age: int

117

skills: List[str] = []

118

119

# Create schema from Pydantic model

120

pydantic_schema = JSONSchema.from_pydantic(PersonInfo)

121

122

request = ChatRequest(

123

model="pharia-1-llm-7b-control",

124

messages=[{

125

"role": "user",

126

"content": "Extract info: Sarah is 25 and specializes in data science and machine learning"

127

}],

128

response_format=pydantic_schema

129

)

130

131

response = client.chat(request)

132

133

# Parse structured response

134

person_data = PersonInfo.model_validate_json(response.message.content)

135

print(f"Name: {person_data.name}, Age: {person_data.age}")

136

print(f"Skills: {', '.join(person_data.skills)}")

137

138

# Direct Pydantic model usage

139

request = ChatRequest(

140

model="pharia-1-llm-7b-control",

141

messages=[{

142

"role": "user",

143

"content": "Analyze this product review and extract key information"

144

}],

145

response_format=PersonInfo # Direct Pydantic model

146

)

147

```