For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Sample appsIntegrationsDiscordPlaygroundDevEx repo
GuidesSDK ReferenceAPI Reference
GuidesSDK ReferenceAPI Reference
  • Python SDK
    • The TwelveLabs class
    • Manage indexes
    • Upload content
    • Index content
    • Manage videos
    • Manage entities
    • Search
    • Create embeddings v2
    • Create embeddings v1
    • Analyze and segment videos
      • Sync analysis
      • Async analysis
  • Node.js SDK
    • The TwelveLabs class
    • Manage indexes
    • Upload content
    • Index content
    • Manage videos
    • Manage entities
    • Search
    • Create embeddings v2
    • Create embeddings v1
    • Analyze and segment videos
LogoLogo
Sample appsIntegrationsDiscordPlaygroundDevEx repo
On this page
  • Sync analysis
  • VideoContext
  • AnalyzePromptV2
  • SmeMediaSource
  • Sync analysis with streaming responses
  • Error codes
Python SDKAnalyze and segment videos

Sync analysis

Was this page helpful?
Previous

Async analysis

Next
Built with

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:

  • Videos longer than 1 hour. Use the analyze_async.tasks.create method instead.
  • Video segmentation with custom segment definitions. Use the analyze_async.tasks.create method with model_name set to "pegasus1.5" instead.

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 with video clipping, structured prompts, longer responses, and video segmentation (async only). See the Pegasus page for details.
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_time − start_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_time − start_time) must be at least 4 seconds.

| response_format | typing.Optional[SyncResponseFormat] | No | Specifies 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_tokens | typing.Optional[int] | No | The maximum response length, in tokens. The allowed range depends on the model: Pegasus 1.2 — Min: 1, Max: 4,096, Default: 4,096. Pegasus 1.5 — Min: 512, Max: 98,304, Default: 4,096. For Pegasus 1.5, the input and response must fit within the context window. | | request_options | typing.Optional[RequestOptions] | No | Request-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?"). For Pegasus 1.5, this text counts toward the context window.
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 the generation stopped. Values:
- null: The generation hasn’t finished yet.
- "stop": The generation reached the end of the output text.
- "length": The response reached the maximum response length or the context window. The partial output is returned and a warning is in the error field. Both Pegasus 1.5 and Pegasus 1.2 report "length". For JSON responses, this may return truncated 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 the input consumed. Together with output_tokens, this value must fit within the context window.

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 with video clipping, structured prompts, longer responses, and video segmentation (async only). See the Pegasus page for details.
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_time − start_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_time − start_time) must be at least 4 seconds.

| response_format | typing.Optional[SyncResponseFormat] | No | Specifies 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_tokens | typing.Optional[int] | No | The maximum response length, in tokens. The allowed range depends on the model: Pegasus 1.2 — Min: 1, Max: 4,096, Default: 4,096. Pegasus 1.5 — Min: 512, Max: 98,304, Default: 4,096. For Pegasus 1.5, the input and response must fit within the context window. | | request_options | typing.Optional[RequestOptions] | No | Request-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 the generation stopped. Values:
- null: The generation hasn’t finished yet.
- "stop": The generation reached the end of the output text.
- "length": The response reached the maximum response length or the context window. The partial output is returned and a warning is in the error field. Both Pegasus 1.5 and Pegasus 1.2 report "length". For JSON responses, this may return truncated 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 the input consumed. Together with output_tokens, this value must fit within the context window.

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
    • The request exceeds the context window and cannot be processed. Reduce the prompt length, use a shorter video, or lower the max_tokens value.
  • output truncated: the generation reached the configured max_tokens. The partial output is returned; raise max_tokens (up to 98304) if you need a longer response.
    • The response reached the maximum response length. The platform returns the partial output and sets finish_reason to "length". This message appears in the error.message field.
  • output truncated: combined input and output tokens reached the model's context limit. The partial output is returned; consider reducing input size (shorter prompt, smaller video clip, fewer media bindings) or lowering max_tokens.
    • The request reached the context window. The platform returns the partial output and sets finish_reason to "length". This message appears in the error.message field.
  • index_not_supported_for_generate
    • You can only summarize videos uploaded to an index with an engine from the Pegasus family enabled.

For a list of general errors that apply to all endpoints, see the Error codes page.