Appends instructions to the system instruction. Args: instructions: The instructions to append. Can be: - list[str]: Strings to append/concatenate to system instruction - types.Content: Content object to append to system instruction Returns: List of user content
(
self, instructions: Union[list[str], types.Content]
)
| 101 | """ |
| 102 | |
| 103 | def append_instructions( |
| 104 | self, instructions: Union[list[str], types.Content] |
| 105 | ) -> list[types.Content]: |
| 106 | """Appends instructions to the system instruction. |
| 107 | |
| 108 | Args: |
| 109 | instructions: The instructions to append. Can be: |
| 110 | - list[str]: Strings to append/concatenate to system instruction |
| 111 | - types.Content: Content object to append to system instruction |
| 112 | |
| 113 | Returns: |
| 114 | List of user contents from non-text parts (when instructions is types.Content |
| 115 | with non-text parts). Empty list otherwise. |
| 116 | |
| 117 | Note: Model API requires system_instruction to be a string. Non-text parts |
| 118 | in Content are processed with references in system_instruction and returned |
| 119 | as user contents. |
| 120 | |
| 121 | Behavior: |
| 122 | - list[str]: concatenates with existing system_instruction using \\n\\n |
| 123 | - types.Content: extracts text parts with references to non-text parts, |
| 124 | returns non-text parts as user contents |
| 125 | """ |
| 126 | |
| 127 | # Handle Content object |
| 128 | if isinstance(instructions, types.Content): |
| 129 | text_parts = [] |
| 130 | user_contents = [] |
| 131 | |
| 132 | # Process all parts, creating references for non-text parts |
| 133 | non_text_count = 0 |
| 134 | for part in instructions.parts: |
| 135 | if part.text: |
| 136 | # Text part - add to system instruction |
| 137 | text_parts.append(part.text) |
| 138 | elif part.inline_data: |
| 139 | # Inline data part - create reference and user content |
| 140 | reference_id = f"inline_data_{non_text_count}" |
| 141 | non_text_count += 1 |
| 142 | |
| 143 | # Create descriptive reference based on mime_type and display_name |
| 144 | display_info = [] |
| 145 | if part.inline_data.display_name: |
| 146 | display_info.append(f"'{part.inline_data.display_name}'") |
| 147 | if part.inline_data.mime_type: |
| 148 | display_info.append(f"type: {part.inline_data.mime_type}") |
| 149 | |
| 150 | display_text = f" ({', '.join(display_info)})" if display_info else "" |
| 151 | reference_text = ( |
| 152 | f"[Reference to inline binary data: {reference_id}{display_text}]" |
| 153 | ) |
| 154 | text_parts.append(reference_text) |
| 155 | |
| 156 | # Create user content with reference and data |
| 157 | user_content = types.Content( |
| 158 | role="user", |
| 159 | parts=[ |
| 160 | types.Part.from_text( |