Specklepy Version: 2.21.2. Create new_version Python instead of commit

Specklepy Version: 2.21.2
Hello! I will need your help…
I’ve noticed, that in the new version of specklepy I couldn’t create a version (commit).

new_commit_id = client.commit.create(
            stream_id=stream_id,
            object_id=obj_id,
            branch_name="main",
            message=f"Updated area and thickness: area={area}, thickness={thickness}"
        )
        return new_commit_id

I’ve tried this method, but this is not correct:

        input_data = {
            "project_id": project_id,
            "object_id": obj_id,
            "branch_name": "main",
            "message": f"Updated area and thickness: area={area}, thickness={thickness}"
        }
        new_version_id = client.version.create(input=input_data)

Is it written in the documentation already? Thank you!

Please see the below example for how to call client.version.create as it is now a little different from the old commit.create

    input = CreateVersionInput(
        objectId=object_id, modelId=model_id, projectId=project_id
    )
    version_id = client.version.create(input)

We’re still yet to update much of our docs, but the type hinting will help you out, if you’re using vscode, make sure you have the python extension enabled.


However, client.commit.create should continue to work, Are you seeing it return an error?

You may need to do some work to adjust your code to pass or fetch model ids rather than branch names. This is the main difference with the new api schema

Thanks! Yes, I’m just working on how to pass the “main” name for the modelId (branch) for the input, as strings are not allowed.
The other problem I’m facing is the ServerTransport. It has stream_id in the arguments… Maybe I’ll manage it, or is there also better way for transport?

So, I think my confusion is comming because the client.commit.create method is not compatible with the new host app.speckle.server anymore.
And I mixed the methods, so now I’m getting 401 error: Failed to create a new version: 401 Client Error: Unauthorized for url

client.commit.create should still work on app.speckle.systems.

If you’re seeing a 401, it may be because you’re trying to use an unauthenticated client. How are you creating your SpeckleClient?


Yep that should be fine, project id and stream id are the same. Some code is still using the old naming for variables.


Yes you will have to make some changes to your code if you want to use the new api functions.

The best solution depends on what you’re wanting to achieve, but you can easily query for all models on a project
e.g project.get_with_models

This function also takes an optional models_filter which you can use to filter only for models with the name “main”

1 Like

Thanks for the answers! I’m using the new server, and than I’m taking the token as env variable:

client = SpeckleClient(host="https://app.speckle.systems")
client.authenticate_with_token(os.getenv("SPECKLE_TOKEN"))

Than, I’m getting the error: Error processing data: 401 Client Error: Unauthorized for url: https://app.speckle.systems/api/diff/f132

some things to check

  1. When you generated your token, did you select the required scopes (streams:read, profile:read)
  2. does that user have permission for the f132... project?
  3. Do you have any accounts added via speckle manager / added to the local accounts db? does it work without these?
  4. Does that os.getenv("SPECKLE_TOKEN") actually return the token you’re expecting?
2 Likes

Hi! I’m still digging in the creating of new version situation. But it says, that some warnings object is making an issue… Thank you for the help!

models_data = client.project.get_with_models(project_id)
model_id = models_data.id

input_data = CreateVersionInput(
                    objectId=obj_id,
                    modelId=model_id,
                    projectId=project_id,
                    warnings=False,
                    message=f"Updated area and thickness: area={area}, thickness={thickness}"
                )
              
                new_version_id = client.version.create(input_data)  
                return new_version_id

Hi,
There’s a couple things I noticed about the code you’ve shared.

1: Properly fetching the model id

The above line is incorrect. project.get_with_models returns a project. so .id will be the project’s id, not the model id.
Have a look at the typing of the return type (ProjectWithModels) and its parent Project class to see all of the properties available.

You’ll need to do something like this if you want the model id.

project = client.project.get_with_models(project_id)

# Gets the first model on the project
model = project.models.items[0]
# OR, if you want to specifically get a model named "main"
model = next(m for m in project.models.items if m.name == "main")
model_id = model.id

2. Calling version.create

See typing of the CreateVersionInput for reference.

The property warnings does not exist, likely is confusing the error message you’re seeing.
CreateVersionInput requires only objectId, modelId, and projectId.


Here is a sample code that I’ve tested. If you follow give this, you should find you have no problems


from specklepy.api.client import SpeckleClient
from specklepy.api.resources.current.version_resource import CreateVersionInput

MY_TOKEN = "XXXX"  # Replace this with your token. It will need atleast "streams:read" and "streams:write" scopes
PROJECT_ID = "c1faab5c62"  # Replace this with your project id.
OBJECT_ID = "321ebe2adb13f8ac79f6903d20813e5f"  # Replace this with your object Id, this object id needs to already have been sent to the project
BRANCH_NAME = "main"

client = SpeckleClient()
client.authenticate_with_token(MY_TOKEN)

project = client.project.get_with_models(PROJECT_ID)
model = next(m for m in project.models.items if m.name == BRANCH_NAME)

input_data = CreateVersionInput(
    objectId=OBJECT_ID,
    modelId=model.id,
    projectId=project.id,
    message="what ever your message is here!",
)

new_version_id = client.version.create(input_data)