Sync analysis

The TwelveLabs class provides methods to analyze videos synchronously and generate text based on their content. These methods return results immediately in the response.

When to use these methods:

  • Analyze videos up to 1 hour
  • Retrieve immediate results without polling for task completion
  • Stream text fragments in real time for immediate processing and feedback

Do not use these methods for:

Sync analysis

Description: This method analyzes your videos and returns the results directly in the response. It generates text based on your prompts and supports both Pegasus 1.2 and Pegasus 1.5 for general analysis (prompt-based text generation).

Note

This method is rate-limited. For details, see the Rate limits page.

Function signature and example:

1def analyze(
2 self,
3 *,
4 model_name: typing.Optional[AnalyzeRequestModelName] = OMIT,
5 video_id: typing.Optional[str] = OMIT,
6 video: typing.Optional[VideoContext] = OMIT,
7 prompt: typing.Optional[AnalyzeTextPrompt] = OMIT,
8 prompt_v_2: typing.Optional[AnalyzePromptV2] = OMIT,
9 temperature: typing.Optional[AnalyzeTemperature] = OMIT,
10 response_format: typing.Optional[SyncResponseFormat] = OMIT,
11 max_tokens: typing.Optional[int] = OMIT,
12 start_time: typing.Optional[float] = OMIT,
13 end_time: typing.Optional[float] = OMIT,
14 request_options: typing.Optional[RequestOptions] = None,
15) -> NonStreamAnalyzeResponse:

Parameters:

NameTypeRequiredDescription
model_nametyping.Optional[AnalyzeRequestModelName]NoThe video understanding model to use for analysis. Values:
- "pegasus1.2": General analysis (prompt-based text generation).
- "pegasus1.5": General analysis (prompt-based text generation) with video clipping, structured prompts with reference images, extended token limits, and video segmentation (async only).
Default: "pegasus1.2"
video_idtyping.Optional[str]NoThe unique identifier of the video to analyze. Use this parameter when the model_name parameter is "pegasus1.2". Not supported with "pegasus1.5". This parameter will be deprecated and removed in a future version. Use the video parameter instead.
videotyping.Optional[VideoContext]NoAn object specifying the source of the video content. Include exactly one source. See VideoContext.
prompttyping.Optional[str]NoA text prompt that guides the model on the desired format or content. Works with both Pegasus 1.2 and Pegasus 1.5. To include reference images in your prompt, use the prompt_v_2 parameter instead (Pegasus 1.5 only). Mutually exclusive with the prompt_v_2 parameter.
prompt_v_2typing.Optional[AnalyzePromptV2]NoA structured prompt with <@name> placeholders for referencing images. Requires the model_name parameter set to "pegasus1.5". Mutually exclusive with the prompt parameter. See AnalyzePromptV2.
temperaturetyping.Optional[float]NoControls the randomness of the text output. Default: 0.2, Min: 0, Max: 1
start_timetyping.Optional[float]NoStart of the analysis window, in seconds. Use with end_time to analyze only a portion of the video. Requires model_name set to "pegasus1.5". If omitted, defaults to 0. Must be less than end_time and less than the video duration. The clip (end_timestart_time) must be at least 4 seconds.
end_timetyping.Optional[float]NoEnd of the analysis window, in seconds. Use with start_time to analyze only a portion of the video. Requires model_name set to "pegasus1.5". If omitted, defaults to the video duration. Must be greater than start_time and less than or equal to the video duration. The clip (end_timestart_time) must be at least 4 seconds.
response_formattyping.Optional[SyncResponseFormat]NoSpecifies the format of the response. When you omit this parameter, the platform returns unstructured text. Only the json_schema type is supported for synchronous analysis.
max_tokenstyping.Optional[int]NoThe maximum number of tokens to generate. The allowed range depends on the model: Pegasus 1.2 — Min: 1, Max: 4,096, Default: 4,096. Pegasus 1.5 — Min: 512, Max: 65,536, Default: 4,096.
request_optionstyping.Optional[RequestOptions]NoRequest-specific configuration.

The SyncResponseFormat class contains the following properties:

NameTypeDescription
typeSyncResponseFormatTypeThe response format to use. Value: "json_schema" (structured JSON conforming to a provided schema).
json_schemaDict[str, Optional[Any]]Contains the JSON schema that defines the response structure. The schema must adhere to the JSON Schema Draft 2020-12 specification. For details, see the json_schema parameter in the API Reference section.

VideoContext

The VideoContext type specifies the source of the video content. Provide exactly one of the following:

ClassFieldTypeDescription
VideoContext_UrlurlstrThe publicly accessible URL of the video file. Use direct links to raw media files. Video hosting platforms and cloud storage sharing links are not supported.
VideoContext_AssetIdasset_idstrThe unique identifier of an asset from a direct or multipart upload. The asset status must be ready. Use assets.retrieve to check the status.
VideoContext_Base64Stringbase64_stringstrThe base64-encoded video data. The maximum size is 30MB.

AnalyzePromptV2

The AnalyzePromptV2 class defines a structured prompt with image references.

NameTypeRequiredDescription
input_textstrYesThe text of the prompt. Use <@name> placeholders to reference images declared in media_sources (Example: "Is there a <@tiger-1> in the video?"). The maximum length is 2,000 tokens.
media_sourcesOptional[List[SmeMediaSource]]NoReference images for the <@name> placeholders in the prompt. Maximum 4 sources. See SmeMediaSource.

SmeMediaSource

A reference image that provides visual context. Provide exactly one of url, asset_id, or base64_string.

NameTypeRequiredDescription
namestrYesA descriptive name for this media source.
media_typeSmeMediaSourceMediaTypeYesThe media type. Value: "image".
urlOptional[str]NoA publicly accessible HTTPS URL of the image.
asset_idOptional[str]NoThe unique identifier of an uploaded asset.
base64_stringOptional[str]NoBase64-encoded image data. The maximum size is 30MB.

Return value: Returns a NonStreamAnalyzeResponse object containing the generated text.

The NonStreamAnalyzeResponse class contains the following properties:

NameTypeDescription
idOptional[str]Unique identifier of the response.
dataOptional[str]The generated text based on the prompt you provided.
finish_reasontyping.Optional[FinishReason]The reason for the generation to finish. Values:
- null: The generation hasn’t finished yet.
- stop: The generation stopped because the model reached the maximum number of tokens. For JSON responses, this may result in truncated, invalid JSON that fails to parse.
usageOptional[TokenUsage]The number of tokens used in the generation.

The TokenUsage class contains the following properties:

NameTypeDescription
output_tokensintThe number of tokens in the generated text.
input_tokensOptional[int]The number of tokens consumed by the input (prompt and video). Omitted for Pegasus 1.5.

The maximum response length is 4,096 tokens for Pegasus 1.2 and up to 65,536 tokens for Pegasus 1.5. Set the max_tokens parameter to change this limit.

API Reference: Sync analysis.

Related guide: Analyze videos.

Sync analysis with streaming responses

Description: This method analyzes your videos and returns the results directly in the response. It generates text based on your prompts and supports both Pegasus 1.2 and Pegasus 1.5 for general analysis (prompt-based text generation).

Note

This method is rate-limited. For details, see the Rate limits page.

Function signature and example:

1def analyze_stream(
2 self,
3 *,
4 model_name: typing.Optional[AnalyzeStreamRequestModelName] = OMIT,
5 video_id: typing.Optional[str] = OMIT,
6 video: typing.Optional[VideoContext] = OMIT,
7 prompt: typing.Optional[AnalyzeTextPrompt] = OMIT,
8 prompt_v_2: typing.Optional[AnalyzePromptV2] = OMIT,
9 temperature: typing.Optional[AnalyzeTemperature] = OMIT,
10 response_format: typing.Optional[SyncResponseFormat] = OMIT,
11 max_tokens: typing.Optional[int] = OMIT,
12 start_time: typing.Optional[float] = OMIT,
13 end_time: typing.Optional[float] = OMIT,
14 request_options: typing.Optional[RequestOptions] = None,
15) -> typing.Iterator[StreamAnalyzeResponse]

Parameters:

NameTypeRequiredDescription
model_nametyping.Optional[AnalyzeRequestModelName]NoThe video understanding model to use for analysis. Values:
- "pegasus1.2": General analysis (prompt-based text generation).
- "pegasus1.5": General analysis (prompt-based text generation) with video clipping, structured prompts with reference images, extended token limits, and video segmentation (async only).
Default: "pegasus1.2"
video_idtyping.Optional[str]NoThe unique identifier of the video to analyze. Use this parameter when the model_name parameter is "pegasus1.2". Not supported with "pegasus1.5". This parameter will be deprecated and removed in a future version. Use the video parameter instead.
videotyping.Optional[VideoContext]NoAn object specifying the source of the video content. Include exactly one source. See VideoContext.
prompttyping.Optional[str]NoA text prompt that guides the model on the desired format or content. Works with both Pegasus 1.2 and Pegasus 1.5. To include reference images in your prompt, use the prompt_v_2 parameter instead (Pegasus 1.5 only). Mutually exclusive with the prompt_v_2 parameter.
prompt_v_2typing.Optional[AnalyzePromptV2]NoA structured prompt with <@name> placeholders for referencing images. Requires the model_name parameter set to "pegasus1.5". Mutually exclusive with the prompt parameter. See AnalyzePromptV2.
temperaturetyping.Optional[float]NoControls the randomness of the text output. Default: 0.2, Min: 0, Max: 1
start_timetyping.Optional[float]NoStart of the analysis window, in seconds. Use with end_time to analyze only a portion of the video. Requires model_name set to "pegasus1.5". If omitted, defaults to 0. Must be less than end_time and less than the video duration. The clip (end_timestart_time) must be at least 4 seconds.
end_timetyping.Optional[float]NoEnd of the analysis window, in seconds. Use with start_time to analyze only a portion of the video. Requires model_name set to "pegasus1.5". If omitted, defaults to the video duration. Must be greater than start_time and less than or equal to the video duration. The clip (end_timestart_time) must be at least 4 seconds.
response_formattyping.Optional[SyncResponseFormat]NoSpecifies the format of the response. When you omit this parameter, the platform returns unstructured text. Only the json_schema type is supported for synchronous analysis.
max_tokenstyping.Optional[int]NoThe maximum number of tokens to generate. The allowed range depends on the model: Pegasus 1.2 — Min: 1, Max: 4,096, Default: 4,096. Pegasus 1.5 — Min: 512, Max: 65,536, Default: 4,096.
request_optionstyping.Optional[RequestOptions]NoRequest-specific configuration.

For details about VideoContext, AnalyzePromptV2, and SmeMediaSource, see the Sync analysis section above.

The SyncResponseFormat class contains the following properties:

NameTypeDescription
typeSyncResponseFormatTypeThe response format to use. Value: "json_schema" (structured JSON conforming to a provided schema).
json_schemaDict[str, Optional[Any]]Contains the JSON schema that defines the response structure. The schema must adhere to the JSON Schema Draft 2020-12 specification. For details, see the json_schema parameter in the API Reference section.

Return value: Returns an iterator of StreamAnalyzeResponse objects. Each response can be a StreamAnalyzeResponse_StreamStart, StreamAnalyzeResponse_TextGeneration, or StreamAnalyzeResponse_StreamEnd.

The StreamAnalyzeResponse_StreamStart class contains the following properties:

NameTypeDescription
event_typetyping.Literal["stream_start"]This field is always set to stream_start for this event.
metadataOptional[StreamStartResponseMetadata]An object containing metadata about the stream.

The StreamAnalyzeResponse_TextGeneration class contains the following properties:

NameTypeDescription
event_typetyping.Literal["text_generation"]This field is always set to text_generation for this event.
textOptional[str]A fragment of the generated text. Text fragments may be split at arbitrary points, not necessarily at word or sentence boundaries.

The StreamAnalyzeResponse_StreamEnd class contains the following properties:

NameTypeDescription
event_typetyping.Literal["stream_end"]This field is always set to stream_end for this event.
finish_reasontyping.Optional[FinishReason]The reason for the generation to finish. Values:
- null: The generation hasn’t finished yet.
- stop: The generation stopped because the model reached the maximum number of tokens. For JSON responses, this may result in truncated, invalid JSON that fails to parse.
metadatatyping.Optional[StreamEndResponseMetadata]An object containing metadata about the stream.

The StreamStartResponseMetadata class contains the following properties:

NameTypeDescription
generation_idOptional[str]A unique identifier for the generation session.

The StreamEndResponseMetadata class contains the following properties:

NameTypeDescription
generation_idOptional[str]The same unique identifier provided in the stream_start event.
usageOptional[TokenUsage]The number of tokens used in the generation.

The TokenUsage class contains the following properties:

NameTypeDescription
output_tokensintThe number of tokens in the generated text.
input_tokensOptional[int]The number of tokens consumed by the input (prompt and video). Omitted for Pegasus 1.5.

The maximum response length is 4,096 tokens for Pegasus 1.2 and up to 65,536 tokens for Pegasus 1.5. Set the max_tokens parameter to change this limit.

API Reference: Sync analysis.

Related guide: Analyze videos.

Error codes

This section lists the most common error messages you may encounter while analyzing videos.

  • token_limit_exceeded
    • Your request could not be processed due to exceeding maximum token limit. Please try with another request or another video with shorter duration.
  • index_not_supported_for_generate
    • You can only summarize videos uploaded to an index with an engine from the Pegasus family enabled.